5、使用WTF创建表单

1、main.py

from flask_wtf import FlaskForm
from flask import Flask,render_template
from wtforms import StringField,PasswordField,SubmitField

app = Flask(__name__)

app.secret_key='suibian'

class LoginForm(FlaskForm):
    username = StringField(u"用户名:")
    password = PasswordField(u'密码:')
    password2 = PasswordField(u'确认密码:')
    submit = SubmitField(u'提交:')

@app.route('/',methods=['GET','POST'])
def loging():
    logings = LoginForm()
    return render_template('myhtml.html',forms = logings)
if __name__=='__main__':
    app.run()

2、myhtml.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>flask框架测试</title>
</head>
<body>
<form method="post">
    <label>用户名:</label><input type="text" name="username"><br>
    <label>密码:</label><input type="password" name="password"><br>
    <label>确认密码:</label><input type="password" name="password2"><br>
    <input type="submit" value="提交"><br>
    {%for message in get_flashed_messages()%}
        {{message}}
    {%endfor%}
</form>
<br>
<form method="post">
    {{forms.username.label}}{{forms.username}}<br>
    {{forms.password.label}}{{forms.password}}<br>
    {{forms.password2.label}}{{forms.password2}}<br>
    {{forms.submit}}<br>
</form>
</body>
</html>
上一篇:flask的线程隔离


下一篇:Flask 项目打包 线上部署