Flask form (Flask-WTF) and flask form flask-wtf

Source: Internet
Author: User

Flask form (Flask-WTF) and flask form flask-wtf

1. request. from: Get POST form data

# Hello. py

1 # coding: UTF-8 2 3 from flask import Flask, request, render_template 4 5 app = Flask (_ name _) 6 7 @ app. route ('/', methods = ['get']) # methods is a list of 8 def home (): 9 return render_template('home.html ') 10 11 @ app. route ('/signin', methods = ['get']) 12 def signin_form (): 13 return render_template('form.html') 14 15 @ app. route ('/signin', methods = ['post']) 16 def signin (): 17 username = request. form ['username'] 18 password = request. form ['Password'] 19 if username = 'admin' and password = 'Password': 20 return render_template('signin-ok.html ', username = username) 21 else: 22 return render_template('form.html ', message = 'user name or Password error. Please log in again> ', username = username) 23 24 25 if _ name _ =' _ main __': 26 app. run ()

# Form.html

1 <! DOCTYPE html> 2 

 

# Home.html

1 <! DOCTYPE html> 2 

#Signin-ok.html

1 <! DOCTYPE html> 2 

2. Use Flask-WTF Extension

# Hello. py

1 # coding: UTF-8 2 3 from flask import Flask, render_template 4 from flask. ext. bootstrap import Bootstrap 5 from flask. ext. wtf import Form 6 from wtforms import StringField, SubmitField 7 from wtforms. validators import Required 8 9 app = Flask (_ name _) 10 app. config ['secret _ key'] = 'hard to guess string '11 bootstrap = Bootstrap (app) 12 13 14 class NameForm (Form ): 15 name = StringField ('What is your name? ', Validators = [Required ()]) 16 submit = SubmitField ('submit') 17 18 @ app. route ('/', methods = ['get', 'post']) 19 def index (): 20 name = None21 form = NameForm () 22 if form. validate_on_submit (): 23 name = form. name. data # does not have this step. name = None24 form after submit. name. data = ''# If this step is not completed, the text box after the submit operation leaves the last input text 25 return render_template('index.html ', form = form, name = name) 26 27 if _ name _ = '_ main _': 28 app. run ()

# Index. py

1 {% extends "base.html" %} 2 {% import "bootstrap/wtf.html" as wtf %} 3 4 {% block title %} Flasky {% endblock %} 5 6 {% block page_content %} 7 <div class = "page-header"> 8 

Appendix: common basic templates

# Base.html

 1 {% extends "bootstrap/base.html" %} 2 {% block title %}Flasky{% endblock %} 3  4 {% block navbar %} 5 <div class="navbar navbar-inverse" role="navigation"> 6 <div class="container"> 7     <div class="navbar-header"> 8         <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> 9             <span class="sr-only">Toggle navigation</span>10             <span class="icon-bar"></span>11             <span class="icon-bar"></span>12             <span class="icon-bar"></span>13         </button>14         <a class="navbar-brand" href="/">Flasky</a>15 16     </div>17     <div class="navbar-collapse collapse">18         <ul class="nav navbar-nav">19             <li><a href="/">Home</a></li>20             <li><a href="/">Twitter</a></li>21             <li><a href="/">Blog</a></li>22         </ul>23     </div>24 </div>25 </div>26 {% endblock %}27 28 29 {% block content %}30 <div class="container">31     <div class="page-header">32         {% block page_content %}{% endblock %}33     </div>34 </div>35 36 {% endblock %}

 

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.