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>

 

posted @ 2020-10-21 19:33  Οo白麒麟оΟ  阅读(189)  评论(0编辑  收藏  举报