For the use of blueprints, you can view the document: Click to enter
First, create a blueprint
1. Create a modules directory in the demo directory, as a directory for the module.
2. In the modules directory, create a home directory as the home module.
3. Create a views.py file in the home directory to write Routing and request processing. (equivalent to Routes and Controllers in ASP. NET MVC)
4. Create a templates directory in the home directory, as a template for the Home module directory, and create an empty index.html page below.
views.py
Homeroute = Blueprint ('home'__name__, url_prefix='/' , template_folder='templates') @homeRoute. Route ('/ ')def index (): return render_template (' index.html')
Note 1:
Homeroute = Blueprint ('home'__name__, url_prefix='/' , template_folder='templates')
This line of code, an object created by Blueprint, can only be used once in the same views.py. That is, you cannot use the object as a public routing object for multiple. py file references.
Second, the registration blueprint
NOTE 2: The blueprint can only be registered in the run.py file if the Create_app () function or the file where the function is registered will have a circular reference to the exception.
Modified full run.py:
#Config=utf-8 fromDemoImportCreate_app fromDemo.modules.home.viewsImportHomeroutedefault_modules=[Homeroute]app= Create_app ('config.py') forModuleinchDEFAULT_MODULES:app.register_blueprint (module) @app. Before_requestdefbefore_request ():"""here is the global method, called before the request begins. Where flask has a global variable G, it is the same as the session, you can use it to save the current user's data Returns:""" Passif __name__=='__main__': App.run ()
Third, about the template search rules
1.Flask will first search the templates directory under the app's root directory.
2.Flask will then search for the templates directory in the directory where the views.py file is located.
Flask found the page in 1, then will not enter 2, in 1 can not find the page, then will go to 2 find, do not find the error.
Various pits encountered when using the Flask framework to write demo of user login (ii)--Modular with blueprint function