在模板中渲染表单
为了能够在模板中渲染表单,我们需要把表单类实例传入模板。首先在视图函数里实例化表单类LoginForm,然后再render_template()函数中使用关键脑子参数form将表单实例传入模板,例如:form/app.py
#传入表单类实例
from forms import LoginForm @app.route('/basic')
def basic():
form=LoginForm()
return render_template('login.html',form=form)
在模板中,需要调用表单类的属性即可获取字段对应的HTML代码,如果需要传入参数,也可以添加括号,如form/templates/basic.html:在模板中渲染表单
<form method="post"> {{ form.csrf_token }}<!-- 渲染CSRF令牌隐藏字段 --> {{ form.username.label }}{{ form.username }}<br> {{ form.password.label }}{{ form.password }}<br> {{ form.remember }}{{ form.remember.label }}<br> {{ form.submit }}<br> </form>
在上面的代码中,除了渲染各个字段的标签和字段本身,还调用了form.csrf_token属性渲染Flask-WTF为表单类自动创建的CSRF令牌字段。form.csrf_token字段包含了自动生成的CSRF令牌值,在提交表单后会自动被验证,为了确保表单通过验证,我们必须在表单中手动渲染这个字段。
Flask_WTF为表单类实例提供了一个form.hidden_tag()方法,这个方法会依次渲染表单中所有的隐藏字段。因为csrf_token字段也是隐藏字段,所有当这个方法被调用时也会渲染csrf_token字段。
渲染后获得的实际HTML代码如下:
<form method="post">
<input id="csrf_token" name="csrf_token" type="hidden" value="ImE1M2QxNDEyNDA2YTE5NGI3NmU1NGY1M2U2ZDkxNjQ2NDc5NTI3ZjYi.XH0xaQ.RvNFnjwMfAiUyiv58pU2xJkQ42g"><!-- 渲染CSRF令牌隐藏字段 -->
<label for="username">Username</label><input id="username" name="username" required type="text" value=""><br>
<label for="password">Password</label><input id="password" name="password" required type="password" value=""><br>
<input id="remember" name="remember" type="checkbox" value="y"><label for="remember">Remember me</label><br>
<input id="submit" name="submit" type="submit" value="Log in"><br>
</form>
运行该程序,访问127.0.0.1:5000/basic可以看到渲染后的表单,页面中的表单和我们在上面使用HTML编写的表单相同
在前面介绍过,使用render_kw字典或是在调用字段时传入参数来定义字段的额外HTML属性,可以通过这种方式添加CSS类,我们编写一个Bootstrap风格的表单:
form/templates/bootstrap.html
{% extends 'base.html' %}
{% block styles %}
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
{% endblock %}
{% block content %}
<h1 class="display-4">Bootstrap Style Form</h1>
<form method="post">
{{ form.csrf_token }}
<div class="form-group">
{{ form.username.label }}
{{ form.username(class='form-control') }}
</div>
<div class="form-group">
{{ form.password.label }}
{{ form.password(class='form-contrl') }}
</div>
<div class="form-check">
{{ form.remember(class='form-check-input') }}
{{ form.remember.label }}
</div>
{{ form.submit(class='btn btn-primary') }}
<form/>
{% endblock content %}
页面*问127.0.0.1:5000/bootstrap
base.html:
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
{% block metas %}
<meta charset="utf-8">
{% endblock metas %}
<title>
{% block title %}
Form - HelloFlask
{% endblock title %}
</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='favicon.ico') }}">
{% block styles %}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
{% endblock styles %}
{% endblock head %}
</head>
<body>
<nav>
{% block nav %}
<ul>
<li><a href="{{ url_for('basic') }}">Home</a></li>
</ul>
{% endblock %}
</nav> <main>
{% for message in get_flashed_messages() %}
<div class="alert">
{{ message }}
</div>
{% endfor %}
{% block content %}{% endblock %}
</main>
<footer>
{% block footer %}
<small> © 2019 <a href="https://www.cnblogs.com/xiaxiaoxu/" title="xiaxiaoxu's blog">夏晓旭的博客</a> /
<a href="https://github.com/xiaxiaoxu/hybridDrivenTestFramework" title="Contact me on GitHub">GitHub</a> /
<a href="http://helloflask.com" title="A HelloFlask project">Learning from GreyLi's HelloFlask</a>
</small>
{% endblock %}
</footer>
{% block scripts %}{% endblock %}
</body>
</html>
style.css文件:
body{
margin:auto;
width:750px;
} img{
max-width:710px;
} nav ul{
list-style-type:none;
margin:;
padding:;
overflow:hidden;
background-color:#333;
} nav li{
float:left;
} nav li.a{
display:block;
color:white;
text-align:center;
padding:14px 16px;
text-decoration:none;
} h1{
color:red;
text-align:center;
padding:14px 16px;
text-decoration:none;
} label{width:200px;display:inline-block;} nav li a:hover{
background-color:#111;
} main{
padding:10px 20px;
} footer{
font-size:13px;
color:#888;
border-top:1px solid #666;
margin-top:25px;
text-align:center;
padding:10px;
} .alert{
position:relative;
padding:0.75rem 1.25rem;
margin-bottom:1rem;
border:1px solid #b8daff;
border-radius:0.25rem;
color:#004085;
background-color: #cce5ff;
} .error{
color:red;
}
app.py:
#encoding=utf-8
from flask import Flask,render_template,flash,redirect,url_for,session
import os app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY','secret string') @app.route('/html',methods=['GET','POST'])
def html():
return render_template('pure_html.html') from forms import LoginForm
#传入表单类实例 @app.route('/basic')
def basic():
form=LoginForm()
return render_template('basic.html',form=form) @app.route('/bootstrap',methods=['GET','POST'])
def bootstrap():
form = LoginForm()
if form.validate_on_submit():
username = form.username.data
flash('welcome home,%s!'% username)
return redirect(url_for('basic'))
return render_template('bootstrap.html', form=form) if __name__ == '__main__':
app.run(debug=True)
forms.py:
#encoding=utf-8
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, Length class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired(), Length(8, 128)])
remember = BooleanField('Remember me')
submit = SubmitField('Log in') from flask import Flask,render_template,flash,redirect,url_for,session
import os app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY','secret string') if __name__ == '__main__':
app.run(debug=True)
如果想手动编写HTML表单的代码,需要注意表单字段的name属性值必须和表单类的字段名称相同,这样在提交表单时WTForms才能正确地获取数据并进行验证。