Flask-WTF 示例

慕宜民
2023-12-01

Flask-WTF 示例


坑请注意

selectField字段提示: choices not valid


area = SelectField(coerce=int)




form.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired, Length, Email, EqualTo


class UserRegisterForm(FlaskForm):
    username = StringField('用户名', [DataRequired('用户名必填!'), Length(min=6, max=20, message='用户名必须介于6-20字符!')])
    password = PasswordField('密码', [DataRequired('密码必填!'), Length(min=6, max=20, message='密码必须介于6-20字符!')])
    confirm = PasswordField('重复密码', [DataRequired('重复密码必填!'), EqualTo('password', message='两次密码输入不一致!')])
    email = StringField('邮箱', [DataRequired('邮箱必填!'), Email('邮箱格式不正确!')])

view.py

class RegView(MethodView):
    def get(self):
        form = UserRegisterForm(request.form)
        return render_template('register.html', form=form)

    def post(self):
        form = UserRegisterForm(request.form)
        if User.query.filter_by(username=form.username.data).first():
            flash("当前用户名已经注册!")
            return render_template('register.html', form=form)
        elif User.query.filter_by(email=form.email.data).first():
            flash("当前邮箱已经注册!")
            return render_template('register.html', form=form)
        else:
            form.validate_on_submit()
            user = User(
                form.username.data,
                form.password.data,
                form.email.data,
            )
            user.save()
            return redirect(url_for('.login'))

jinja2 宏方法    helper.html    获取 flask的 flash 消息

{% macro renden_field(field) %}
    {% if field.errors %}
        {% for error in field.errors %}
            <li style="font-size: medium">{{ error }}</li>
        {% endfor %}
    {% endif %}
{% endmacro %}

{% macro messages(get_flashed_messages) %}
    {% with messages = get_flashed_messages() %}
        {% if messages %}
            {% for message in messages %}
            <div class="alert alert-danger">
                <a href="#" class="close" data-dismiss="alert">×</a>
                {{ message }}
            </div>
            {% endfor %}
        {% endif %}
    {% endwith %}
{% endmacro %}




form.html

{% from 'helper.html' import renden_field,messages %}
        <form action="" class="form-horizontal" method="post">
            {{ form.csrf_token }}
            {{ messages(get_flashed_messages) }}
            <div class="panel-body">
                <div class="form-group">
                    {{ form.username.label(class="col-lg-2 col-sm-2 control-label") }}
                    <div class="col-md-6">
                        {{ form.username(class='from-control') }}
                        {{ renden_field(form.username) }}
                    </div>
                </div>
                <div class="form-group">
                    {{ form.password.label(class="col-lg-2 col-sm-2 control-label") }}
                    <div class="col-md-6">
                        {{ form.password(class='from-control') }}
                        {{ renden_field(form.password) }}
                    </div>
                </div>




 
 类似资料: