Python lightweight web framework flask use

Source: Internet
Author: User

http://blog.csdn.net/jacman/article/details/49098819

Directory (?) [+]

    1. Flask Installation
    2. Python Development Tools Eclipsepydev Preparation
    3. Flask Project Framework Construction
    4. MVC Pattern Instance Development
      1. Login instance

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 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 slower students) 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 When installing, select the PIP and add Python.exe to path options, such as 2. Install the Flask framework to open a 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
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
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, Then move the files under the features folder to the D:\dev\eclipse\features directory and move the files under the Plugins folder to the D:\dev\eclipse\plugins directory 3. Restart Eclipse in windows- >preferences The PyDev configuration item appears, indicating that the PyDev plug-in installation was successful 4. Configure PyDev Windows->preferences->pydev->interpreter-python, New A Python interpreter, fill in the Interpreter name and path, the path to select the corresponding Python.exe so far, pydev configuration is done

Flask Project Framework Construction

1. New Python Project File->new->pydev Project 2. New project Structure folder app– root directory static– static resource directory, picture, js,css templates– template _init_.py– init Script views.py– View Controller tmp– temporary folder run.py– Project Launcher 3. Create the magical Hello World program flask. PY by _init_ to create a flask instance

from flask import flashapp = Flask(__name__)from app import views
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

run.py Creating a startup script

from app import appapp.run(debug = True)
    • 1
    • 2
    • 1
    • 2

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

from app import app@app.route("/")def index(): return "hello,world!"
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

4. Start the program

python run.py
    • 1
    • 1

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

MVC Pattern Instance Development

Controller uses an introduction to intercept/index requests

@app.route("/index")def index(): return ‘index‘
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

Block Post or Get methods

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

RESTful support

@app.route("/index/<username>")def index(username): return username
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3
Login Instance

We use a login example to show the full MVC process 1. Create LoginForm login Form app-> new forms.py, Inherit 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()])
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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>    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

3. Login Controller

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)             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

4. Login Success Page index.html

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

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

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.