Flask uses a blueprint to separate routes and write them in different file instances for parsing.

Source: Internet
Author: User
Tags subdomain subdomain name

Flask uses a blueprint to separate routes and write them in different file instances for parsing.

The content of this article is mainly about how to use a blueprint in flask to separate routes and write them in different files. The details are as follows.

Flask uses the concept of blueprints to create application components and support general patterns in an application or across applications. The blueprint simplifies the way large applications work, and provides the Flask extension with the core method for registering operations on applications. A Blueprint object works like a Flask application object, but it is not an application, but a Blueprint that describes how to build or expand an application.

The blueprints in Flask are designed for these situations:

Splits an application into a set of blueprints. This is ideal for large applications. A project can instantiate an application object, initialize several extensions, and register a set of blueprints.

Register a blueprint on the application with the URL prefix and/or subdomain name. The parameters in the URL prefix/subdomain name are the common view parameters of all view functions under the Blueprint (by default ).

Register a blueprint multiple times with different URL rules in an application.

Provides template filters, static files, templates, and other functions through blueprints. A Blueprint does not have to implement applications or view functions.

Register a blueprint in these cases when initializing a Flask extension.

The blueprint in Flask is not an plug-and-play application, because it is not actually an application-it can be registered, or even can be registered multiple times to the operation set on the application. Why not use multiple application objects? You can do that (see Application Scheduling), but your application configurations are separated and managed at the WSGI layer.

As an alternative to the Flask layer, the blueprint shares application configurations and can change registered application objects if necessary. Its disadvantage is that you cannot cancel registering a blueprint after an application is created without destroying the entire application object.

The above content is from the Flask0.10.1 document

TEST project structure:

User. py:

#coding:utf-8 #user from flask import Blueprint, render_template, redirect user = Blueprint('user',__name__)  @user.route('/index') def index():   return render_template('user/index.html') @user.route('/add') def add():   return 'user_add' @user.route('/show') def show():   return 'user_show' 

Admin. py:

# coding:utf-8 #admin.py from flask import Blueprint,render_template, request admin = Blueprint('admin',__name__)  @admin.route('/index') def index():   return render_template('admin/index.html') @admin.route('/add') def add():   return 'admin_add' @admin.route('/show') def show():   return 'admin_show' 

View. py:

# coding:utf-8 from flask import Flask,request,render_template from admin.admin import admin from user.user import user app=Flask(__name__) app.register_blueprint(admin,url_prefix='/admin') app.register_blueprint(user, url_prefix='/user') if __name__ == '__main__':   app.run() 

Summary

The above section describes how to use a blueprint in flask to separate routes and write them into all the content parsed by different file instances. I hope this will help you. Interested friends can continue to refer to this site:

Flask uses session to save logon status and intercept Unlogged Request Code"

Talking about flask intercepting all access and before/after_request Modifier"

Sample Code for actively throwing exceptions and handling exceptions in flask"

If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

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.