First give the Flask Chinese document address: http://docs.jinkan.org/docs/flask/
1 Web Development Basics:
Front-end development:
MVC design Pattern:
http
Based on the request corresponding mode
No status
Request method: GET, POST, Delete,put
Basic components of the flask application:
# Coding=utf-8from Flask Import Flask # import Class app = Flask (__name__) # Instantiate Flask instance @app.route ('/') # The routing portion of the app, given the following view function Urldef Hello_world (): Return ' Hello world! ' if __name__ = = ' __main__ ': # Run App.run ()
Routing and Reverse routing flaskapp.py:
# coding=utf-8from flask import flask, request, url_for # Import class App = flask (__name__) # Instantiate Flask instance @app.route ('/') # the routing portion of the app, For the following view function give Urldef hello_world (): return ' hello world! ' @app. Route ('/user/<id> ') # , methods=[' POST ') def hello_user (ID): return ' hello user! ' + id@app.route ('/query_user ') # access:http://127.0.0.1:5000/query_user?id=2def Query_user (): id = request.args.get (' id ') return ' Query_user ' + id# reverse route @app.route ('/query_url ') # http://127.0.0.1:5000/query_ url shown as: Query url/query_userdef query_url (): return ' Query url ' + url_for (' query_user ') if __name__ == ' __main__ ': # run &NBSP;&NBSp; app.run ()
Flash templates:
Find Jinja2 a python template engine: Https://github.com/mitsuhiko
Visit http://jinja.pocoo.org/
Flack Rendering:
To create a index.html file:
<! DOCTYPE html>
Add in flaskapp.py:
@app. Route ('/') # The routing portion of the app, given Urldef Hello_world () for the following view function: content = "Hello World" return render_template ("index.htm L ", content=content)
If a complex object is passed in:
models.py
Class User (object): Def __init__ (self, user_id, user_name): self.user_id = user_id Self.user_name = user_ Name
User_index.html:
<! DOCTYPE html>
To set up a route:
@app. Route ('/user ') def user_index (): User = User (1, ' Thystar ') return render_template ("user_index.html", User=user)
Conditional statements in a template:
def q_user (user_id): User = None if int (user_id) = = 1:user = User (1, ' Thystar ') return render_template ("U Ser_id.html ", User=user)
User_id.html
<! DOCTYPE html>
Exception handling:
@app. ErrorHandler (404) def not_found (e): Return render_template ("404.html")
404.HTML:
<! DOCTYPE html>
Another way is:
@app. Route ('/user/<id> ') #, methods=[' POST ') def hello_user (ID): if int (id) = = 1:return Render_templat E ("index.html") Else:abort (404)
Finally, mention the inheritance of the template:
In some cases, when a Web page is toggled, some pages do not change, such as the navigation bar, so in order to avoid code duplication, use inheritance: Invariant parts are implemented in the parent class, and the changed parts are implemented in subclasses
First, define the base class Base.html:
<! DOCTYPE html>
Then define two subclasses of ont_base.html and two_base.html
<!--inheriting base class-->{% extends "base.html"%}{% block content%}
<!--inheriting base class-->{% extends "base.html"%}{% block content%}
Add in flaskapp.py:
@app. Route ('/one ') def one_base (): Return Render_template ("one_base.html") @app. Route ('/two ') def two_base (): Return re Nder_template ("two_base.html")
Open Browser test: Http://127.0.0.1:5000/two
Geek College: Http://www.jikexueyuan.com/course/943_3.html?ss=1
Python Learning Notes (4)---Introduction to flask development