Flask Notes: 4:web form

Source: Internet
Author: User
Tags openid

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.py
From 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.py
From 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.