Python lightweight web framework flask use

Source: Internet
Author: User

The Python language has become more and more popular in recent years, characterized by rapid development, simple syntax, portability and so on. I wrote Demo:https://github.com/tangthis/python3_demo on Python3 Basic grammar, which can be consulted as basic tutorials and manuals.

The Python Web development framework commonly includes django,flask,tornado,bottle,web.py,quixote and so on. The Django feature is comprehensive, relatively heavy, relatively, flask more lightweight, suitable for beginners to learn and get started. Below is an introduction to the basic use of flask, including the following:

          • Flask Installation
          • Python Development Tools Eclipsepydev Preparation
          • Flask Project Framework Construction
          • MVC Pattern Instance Development
            • Login instance

Flask Installation

1. Install the python3 
Download the Python 3.4 corresponding 64-bit installer or 32-bit installer from the official Python website (please visit the local mirror for the students with slow speed)  
64-bit: https:// www.python.org/ftp/python/3.4.3/python-3.4.3.amd64.msi 
32-bit: https://www.python.org/ftp/python/3.4.3/ python-3.4.3.msi 
Domestic Mirror: http://pan.baidu.com/s/1sjqOkFF 
to select Pip and add Python.exe to when installing Path options, such as  
 
2. Install flask frame  
Open the Named Row window and execute the following command

install flask  pip install flask-login  pip install flask-openid  pip install flask-sqlalchemy  pip install sqlalchemy-migrate  pip install flask-whooshalchemy  pip install flask-wtf  pip install flask-babel  pip install flup

Python Development Tools Eclipse+pydev Preparation

1. Download the Pythondev plugin
http://sourceforge.net/projects/pydev/
2. After extracting the Pydev plugin, you will find features, plugins two folders, and then move the files under the features folder to the D:\dev\eclipse\features directory and move the files under the Plugins folder to D:\dev\ Under the Eclipse\plugins directory
3. Restart Eclipse, the Pydev configuration item appears in Windows->preferences, indicating that the Pydev plugin was successfully installed
4. Configure Pydev
Windows->preferences->pydev->interpreter-python,new a Python interpreter, fill in the Interpreter name and path, and select the appropriate Python.exe


At this point, the Pydev configuration is finished

Flask Project Framework Construction

1. Create a new Python project
File->new->pydev Project
2. Create a new project Structure folder

app– root directory
static– static Resource directory, pictures, JS,CSS, etc.
templates– templates
  _init_. py– init Script
views.py– View Controller
tmp– Temp Folder
run.py– Project Startup program
3. Create the Magic Hello World program through flask
_init_. Py Creating an Flask instance

from flask import flashapp = Flask(__name__)from app import views

run.py Creating a startup script

from app import appapp.run(debug = True)

In the app directory, create views.py and add the following code

from app import app@app.route("/")def index(): return "hello,world!"

4. Start the program

python run.py

5. Visit http://localhost:5000/and return to Hello,world

MVC Pattern Instance Development

Controller Usage Introduction
Intercept/index Requests

@app.route("/index")def index(): return ‘index‘

Block Post or Get methods

@app.route("/index",methods = [‘GET‘])def index(): return ‘index‘

RESTful support

@app.route("/index/<username>")def index(username): return username

Login instance

We use a login example to show the full MVC process
1. Create a LoginForm login form
App-> new forms.py, inheriting Flask form class

from flask.ext.wtf import Formfrom wtforms import TextField,PasswordFieldfrom wtforms.validators import Required,Lengthclass LoginForm(Form): username= TextField(‘username‘,validators = [Required()]) password = PasswordField(‘username‘,validators = [Required()])

2. Login Page login.html

App->template-> New login.html

<h1> Login</H1><Formaction=""Method="POST"Name="Login" ><p> Please enter your user name:<Br>{{Form.username (SIZE=30)}}<Br></p> < p> Please enter user name: <br> {{form.password (size=30)}} <br> </ p> <p> <input type= "submit" value= "login" ></ p></FORM>    

3. Log in controller controllers

from app import app @app. Route ( '/login ', methods = [' GET ', ' POST ']) def login ():  #接收参数 form = LoginForm () Span class= "hljs-comment" > #提交验证 if form.validate_on_submit ():  #打印 (PS: Validation logic self-complements) print ( ' username: ' + form.username.data + ' Span class= "hljs-string", Password: ' + form.password.data)  #返回到index. html page return render_template ( ' index.html ', form = form)  #未提交 return render_template ( Login.html ', form = form)             

4. Login Success Page index.html

<h1>Hi,{{form.username}}!</h1>

With the above steps, a simple login logic developed using flask can be seen as a high-efficiency development. For more flask content, please download the GitHub Weibo Weibo instance source

GitHub Source: Https://github.com/tangthis/Weibo

Python lightweight web framework flask use

Related Article

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.