processing Web Form forms requires FLASK-WTFCreate a configuration file at the root directorymyblog/config.py
csrf_enabled=truesecret_key= ' you-will-never-guess '
csrf_enabled is configured to activate cross-site request forgery ProtectionSecret_key is when CSRF is activated, an encryption token is established to validate the form
Modify app/__init__.py
From flask import Flaskapp=flask (__name__) app.config.from_object (' config ') read configuration file from app import views#
write the first formapp/forms.py
From FLASK.EXT.WTF import formfrom wtforms import stringfield,booleanfieldfrom wtforms.validators Import Datarequiredclass LoginForm (Form): OpenID = Stringfield (' OpenID ', validators=[datarequired ()]) Remember_me = Booleanfield (' Remember_me ', default=false)
Import the required modulesCreate a Form classDefine an OpenID text box (Stringfield) with the name OpenID and need to verify if it is empty validators=[datarequired ()]Define a Remember_me selection box (Booleanfield), name Remember_me, default is False
Create a form templateapp/templates/login.html
{% extends "base.html"%} {% block content%}
{{Form.hidden_tag ()}} is used to implement CSRF protection in the configuration, and if CSRF is activated, this field will appear in all forms{{Form.openid (size=80)}}} The OpenID field of form forms, the size of the input box
Form ViewModify app/views.pyFrom flask import render_templatefrom App import appfrom. Forms Import Loginform@app.route ('/') @app. Route ('/index ') def Index (): user={' nickname ': ' Bob '} posts=[ {' author ': {' nickname ': ' John '}, ' body ': ' Beautiful day in Portland! '}, {' author ': {' nickname ': ' Susan '}, ' body ': ' The Avengers movie was so cool! '} ] Return Render_template ("index.html", title= "Home", User=user, posts=posts) @app. Route ('/login ', methods=[' GET ', ' POST '] def login (): form = LoginForm () return render_template ("login.html", title = " Sign in ", form = form) #导入LoginForm, view accepts get and POST request # instantiate a LoginForm
Receive form dataModify app/views.pyFrom flask import render_template,flash,redirectfrom App import appfrom. Forms Import Loginform@app.route ('/') @ App.route ('/index ') def index (): user={' nickname ': ' Bob '} posts=[ {' author ': {' nickname ': ' John '}, ' Body ': ' Beautiful day in Portland! '}, {' author ': {' nickname ': ' Susan '}, ' body ': ' The Avengers movie is so cool! '} ] Return Render_template ("index.html", title= "Home", User=user, posts=posts) @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) #form. Validate_on_submit ( ) to determine whether to commit the # after submission with Flash () to pass the data #redirect () is a URL jump
Modify App/templates/base.html
Modify App/templates/login.html{% extends "base.html"%} {% block content%}
Display:do not enter a direct commit will show an error, this is the program comes with the error, that is form.openid.errorsEnter Bob in the input box and select Remember_me
display the submitted content on the index page
Flask Notes: 4:web form