Flask Quick Start (16) - a blueprint for an object and g

Role: a program directory structure is divided

Do not use a blueprint for their own sub-file

Directory Structure:

-templates
-views
    -__init__.py
    -user.py
    -order.py
-app.py

app.py

from views import app
if __name__ == '__main__':
    app.run()

init.py

from flask import Flask,request
app = Flask(__name__)
#不导入这个不行
from . import order
from . import user

user.py

from . import app
@app.route('/user')
def user():
    return 'user'

order.py

from . import app
@app.route('/order')
def order():
    return 'order'

Use small and medium systems blueprint

Directory Structure

-flask_pro
    -flask_test
        -__init__.py
        -static
        -templates
        -views
            -order.py
            -user.py
     -manage.py

init.py

from flask import  Flask
app=Flask(__name__)
from flask_test.views import user
from flask_test.views import order
app.register_blueprint(user.us)
app.register_blueprint(order.ord)

manage.py

from flask_test import  app
if __name__ == '__main__':
    app.run(port=8008)

user.py

from flask import Blueprint
us=Blueprint('user',__name__)

@us.route('/login')
def login():
    return 'login'

order.py

from flask import Blueprint
ord=Blueprint('order',__name__)

@ord.route('/test')
def test():
    return 'order test'

to sum up:

XXX = the Blueprint. 1 ( 'Account', name , url_prefix = '/ XXX'): Blueprint URL prefixes Prefix indicating the url, in this blueprint are all prefix url

2 xxx = Blueprint ( 'account', name, url_prefix = '/ xxx', template_folder = 'tpls'): to the current blueprint templates used alone, look up the current fails, you will always find templates

3 blueprint befort_request, valid for the current blueprint

4 large projects, can be simulated in the app concept is similar to django

g Object

g the object designed to store the user information, g is called the global. Where all the code g target in a request, which may be used are

G distinguish objects, flash, and the session

  • g Objects: Once set, can only be acquired in the current request, other requests can not be acquired
  • flash: Once set, available on request at any one time, but can only take one
  • session: just set, can get in any request, no matter how many times you get

Guess you like

Origin www.cnblogs.com/863652104kai/p/11707327.html