Forms are the fundamental element that allows users to interact with our web applications. Flask itself doesn't help us with forms, but the Flask-WTF extension lets us use the popular WTForms package in our Flask applications. This package makes defining forms and handling submissions easier.
Flask-WTF
The first thing we want to do with Flask-WTF (after installing it, GitHub project page: https://github.com/lepture/flask-wtf) is to define a form in the myapp.forms package.
# ourapp/forms.py from flask_wtf import Form from wtforms import StringField, PasswordField from wtforms.validators import DataRequired, Email class EmailPasswordForm(Form): email = StringField('Email', validators=[DataRequired(), Email()]) password = PasswordField('Password', validators=[DataRequired()])
Before Flask-WTF version 0.9, Flask-WTF provided its own packaging for WTForms fields and validators. You may see a lot of code outside that imports TextField and PasswordField from flask.ext.wtforms rather than from wtforms.
After Flask-WTF version 0.9, we should import these fields and validators directly from wtforms.
The form we define is a user login form. We call it EmailPasswordForm(), and we can reuse this same form class (Form) to do other things, like registration forms. Here we do not define a long and useless form, but choose a very commonly used form, just to introduce to you how to use Flask-WTF to define a form. Maybe a particularly complex form will be defined in a formal project in the future. For forms that include field names, we recommend using a name that is clear and unique within a form. It has to be said that for a long form, we may have to give a field name that is more consistent with the above.
Login forms can do a few things for us. It secures our application against CSRF vulnerabilities, validates user input and renders appropriate markup for the fields we define for our forms.
CSRF Protection and Verification
CSRF stands for Cross-Site Request Forgery. A CSRF attack occurs when a third party forges a request (like a form submission) to an application's server. A vulnerable server assumes that data from a form comes from its own website and acts accordingly.
As an example, let's say an email provider lets you delete your account by submitting a form. The form sends a POST request to the account_delete endpoint on the server and deletes the logged in account when the form is submitted. We can create a form on our website that sends a POST request to the same account_delete endpoint. Now, if we let someone click the submit button on our form (or do so via JavaScript), the login provided by the email provider will be removed. Of course the email provider doesn't know yet that the form submission is not happening on their website.
So how can I prevent POST requests from coming from other websites? WTForms makes this possible by generating a unique token when rendering each form. The generated token is passed back to the server along with the data in the POST request, and the token must be validated by the server before the form can be accepted. The key thing is that the token is associated with a value stored in the user's session (cookies) and expires after a certain amount of time (default is 30 minutes). This way you ensure that the person who submits a valid form is the person who loaded the page (or at least the same person using the same computer), and they can only do so within 30 minutes of loading the page.
To start protecting CSRF with Flask-WTF, we need to define a view for our login page.
# ourapp/views.py from flask import render_template, redirect, url_for from . import app from .forms import EmailPasswordForm @app.route('/login', methods=["GET", "POST"]) def login(): form = EmailPasswordForm() if form.validate_on_submit(): # Check the password and log the user in # [...] return redirect(url_for('index')) return render_template('login.html', form=form)
If the form has been submitted and verified, we can continue with the login logic. If it has not been submitted (for example, just a GET request), we have to pass the form object to our template so that it can be rendered. Here is what the template looks like when we use CSRF protection.
{# ourapp/templates/login.html #} {% extends "layout.html" %} {% endraw %} <html> <head> <title>Login Page</title> </head> <body> <form action="{{ url_for('login') }}" method="post"> <input type="text" name="email" /> <input type="password" name="password" /> {{ form.csrf_token }} </form> </body> </html>
{% raw %}{{ form.csrf_token }}{% endraw %} renders a hidden field that contains those fancy CSRF tokens, and looks for this field when WTForms validates the form. We don’t have to worry about including the logic to handle the token, WTForms will do it for us proactively. Hurrah!
Custom verification
In addition to the built-in form validators provided by WTForms (e.g., Required(), Email(), etc.), we can create our own validators. We'll illustrate how to create your own validator by writing a Unique() validator that checks the database and ensures that the user-supplied value does not exist in the database. This can be used to ensure that the username or email address is not already in use. Without WTForms we might have to do these things in the view, but now we can do things in the form itself.
Now let’s define a simple registration form. In fact, this form is almost the same as the login form. We'll just add some custom validators to it later.
# ourapp/forms.py from flask_wtf import Form from wtforms import StringField, PasswordField from wtforms.validators import DataRequired, Email class EmailPasswordForm(Form): email = StringField('Email', validators=[DataRequired(), Email()]) password = PasswordField('Password', validators=[DataRequired()])
现在我们要添加我们的验证器用来确保它们提供的邮箱地址不存在数据库中。我们把这个验证器放在一个新的 util 模块,util.validators。
# ourapp/util/validators.py from wtforms.validators import ValidationError class Unique(object): def __init__(self, model, field, message=u'This element already exists.'): self.model = model self.field = field def __call__(self, form, field): check = self.model.query.filter(self.field == field.data).first() if check: raise ValidationError(self.message)
这个验证器假设我们是使用 SQLAlchemy 来定义我们的模型。WTForms 期待验证器返回某种可调用的对象(例如,一个可调用的类)。
在 Unique() 的 \_\_init\_\_ 中我们可以指定哪些参数传入到验证器中,在本例中我们要传入相关的模型(例如,在我们例子中是传入 User 模型)以及要检查的字段。当验证器被调用的时候,如果定义模型的任何实例匹配表单中提交的值,它将会抛出一个 ValidationError。我们也可以添加一个具有通用默认值的消息,它将会被包含在 ValidationError 中。
现在我们可以修改 EmailPasswordForm,使用我们自定义的 Unique 验证器。
# ourapp/forms.py from flask_wtf import Form from wtforms import StringField, PasswordField from wtforms.validators import DataRequired from .util.validators import Unique from .models import User class EmailPasswordForm(Form): email = StringField('Email', validators=[DataRequired(), Email(), Unique( User, User.email, message='There is already an account with that email.')]) password = PasswordField('Password', validators=[DataRequired()])
渲染表单
WTForms 也能帮助我们为表单渲染成 HTML 表示。WTForms 实现的 Field 字段能够渲染成该字段的 HTML 表示,所以为了渲染它们,我们只必须在我们模板中调用表单的字段。这就像渲染 csrf_token 字段。下面给出了一个登录模板的示例,在里面我们使用 WTForms 来渲染我们的字段。
{# ourapp/templates/login.html #} {% extends "layout.html" %} <html> <head> <title>Login Page</title> </head> <body> <form action="" method="post"> {{ form.email }} {{ form.password }} {{ form.csrf_token }} </form> </body> </html>
我们可以自定义如何渲染字段,通过传入字段的属性作为参数到调用中。
<form action="" method="post"> {{ form.email.label }}: {{ form.email(placeholder='yourname@email.com') }} <br> {% raw %}{{ form.password.label }}: {{ form.password }}{% endraw %} <br> {% raw %}{{ form.csrf_token }}{% endraw %} </form>
处理 OpenID 登录
现实生活中,我们发现有很多人都不知道他们拥有一些公共账号。一部分大牌的网站或服务商都会为他们的会员提供公共账号的认证。举个栗子,如果你有一个 google 账号,其实你就有了一个公共账号,类似的还有 Yahoo, AOL, Flickr 等。
为了方便我们的用户能简单的使用他们的公共账号,我们将把这些公共账号的链接添加到一个列表,这样用户就不用自手工输入了。
我们要把一些提供给用户的公共账号服务商定义到一个列表里面,这个列表就放到配置文件中吧 (fileconfig.py):
CSRF_ENABLED = True SECRET_KEY = 'you-will-never-guess' OPENID_PROVIDERS = [ { 'name': 'Google', 'url': 'https://www.google.com/accounts/o8/id' }, { 'name': 'Yahoo', 'url': 'https://me.yahoo.com' }, { 'name': 'AOL', 'url': 'http://openid.aol.com/<username>' }, { 'name': 'Flickr', 'url': 'http://www.flickr.com/<username>' }, { 'name': 'MyOpenID', 'url': 'https://www.myopenid.com' }]
接下来就是要在我们的登录视图函数中使用这个列表了:
@app.route('/login', methods = ['GET', 'POST']) def login(): form = LoginForm() if form.validate_on_submit(): flash('Login requested for OpenID="' + form.openid.data + '", remember_me=' + str(form.remember_me.data)) return redirect('/index') return render_template('login.html', title = 'Sign In', form = form, providers = app.config['OPENID_PROVIDERS'])
我们从 app.config 中引入了公共账号服务商的配置列表,然后把它作为一个参数通过 render_template 函数引入到模板。
接下来要做的我想你也猜得到,我们需要在登录模板中把这些服务商链接显示出来。
<!-- extend base layout --> {% extends "base.html" %} {% block content %} <script type="text/javascript"> function set_openid(openid, pr) { u = openid.search('<username>') if (u != -1) { // openid requires username user = prompt('Enter your ' + pr + ' username:') openid = openid.substr(0, u) + user } form = document.forms['login']; form.elements['openid'].value = openid } </script> <h1>Sign In</h1> <form action="" method="post" name="login"> {{form.hidden_tag()}} <p> Please enter your OpenID, or select one of the providers below:<br> {{form.openid(size=80)}} {% for error in form.errors.openid %} <span style="color: red;">[{{error}}]</span> {% endfor %}<br> |{% for pr in providers %} <a href="javascript:set_openid('{{pr.url}}', '{{pr.name}}');">{{pr.name}}</a> | {% endfor %} </p> <p>{{form.remember_me}} Remember Me</p> <p><input type="submit" value="Sign In"></p> </form> {% endblock %}
这次的模板添加的东西似乎有点多。一些公共账号需要提供用户名,为了解决这个我们用了点 javascript。当用户点击相关的公共账号链接时,需要用户名的公共账号会提示用户输入用户名, javascript 会把用户名处理成可用的公共账号,最后再插入到 openid 字段的文本框中。
下面这个是在登录页面点击 google 链接后显示的截图: