Various pits encountered when using the Flask framework to write demo of user login (ii)--Modular with blueprint function

Source: Internet
Author: User

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

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.