Form组件参数配置
# 定制模板中的显示样式,及配置类 # widget=widgets.PasswordInput(attrs={'class': 'form-control'}) # 错误信息中文显示 error_messages={'min_length': '太短了小伙子'} class MyForm(forms.Form): # 校验这个字段,最大长度是32,最小长度是3 name = forms.CharField(required=False, max_length=32, min_length=3, label='用户名', widget=widgets.TextInput(attrs={'class': 'form-control'}), error_messages={'min_length': '太短了小伙子'}) password = forms.CharField(required=False, max_length=32, min_length=3, label='密码', widget=widgets.PasswordInput(attrs={'class': 'form-control'}), error_messages={'min_length': '太短了小伙子'}) re_password = forms.CharField(required=False, max_length=32, min_length=3, label='确认密码', widget=widgets.PasswordInput(attrs={'class': 'form-control'}), error_messages={'min_length': '太短了小伙子'}) email = forms.EmailField(label='邮箱', error_messages={'required': '小惠子,这个必填'}, widget=widgets.TextInput(attrs={'class': 'form-control'})) age = forms.IntegerField(max_value=200, min_value=0, label='年龄', widget=widgets.TextInput(attrs={'class': 'form-control'})) text = forms.CharField(label='个人简介', widget=widgets.Textarea(attrs={'class': 'form-control'})) date = forms.CharField(label='出生日期', widget=widgets.DateInput(attrs={'class': 'form-control'}))
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css"> <title>Title</title> </head> <body> <hr> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h1>半自动渲染模板3(用的最多)</h1> <form action="" method="post" novalidate> {% for foo in form %} <div class="form-group"> <label for="">{{ foo.label }}</label> {{ foo }} <span class="text-danger pull-right">{{ foo.errors }}</span> </div> {% endfor %} <div class="text-center"> <input type="submit" value="提交" class="btn btn-danger"> <span class="text-primary">{{ err }}</span> </div> </form> </div> </div> </div> </body> </html>
每天逼着自己写点东西,终有一天会为自己的变化感动的。这是一个潜移默化的过程,每天坚持编编故事,自己不知不觉就会拥有故事人物的特质的。 Explicit is better than implicit.(清楚优于含糊)