Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Python Flask framework

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use the Python Flask framework". In daily operation, I believe many people have doubts about how to use the Python Flask framework. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about how to use the Python Flask framework. Next, please follow the editor to study!

Catalogue

As the Flask project becomes more and more complex, putting all the view functions in one application file will be very inconvenient for us to manage, so we need to modularize the program. Flask has a built-in class for modular processing-- the Blueprint blueprint.

Blueprint is a way to organize a set of related views and other code. Instead of registering views and other code directly with the application, blueprints register them with the blueprint, and then register the blueprint with the application in the factory function.

To put it simply, divide the app application into many small modules, and put these modules in a package, which is called blueprint, as shown in the following figure:

The project directory is shown in the following figure:

Here we have created a package called apps, and our package is called Blueprint. In this package, we have created three more packages, so how do we relate the blueprint to app.py. At this point, we need to use the init.py initialization file to compare the blueprint with app.py.

First, we create a view function file named view in the user package, with the following code:

From flask import Blueprintuser_bp= Blueprint ('user',__name__)

First import the Blueprint package, and then use the Blueprint () method to define the variable user_bp as the blueprint object.

The init.py initialization file code is as follows:

From flask import Flaskfrom apps.user.view import user_bpdef create_app (): app=Flask (_ _ name__,template_folder='../templates',static_folder='../static') # Blueprint app.register_blueprint (user_bp) return app

First we import the view object user_bp in user, customize the function create_app () and create an app object, and then bind the blueprint object to the app object through the app.register_blueprint () method.

Note: since the init.py initialization file is not at the same level as the static static file and the templates template file, you need to reassign the location of the static file and the template file.

Next, write the app.py startup file with the following code:

From flask import Flaskfrom apps.user.view import user_bpdef create_app (): app=Flask (_ _ name__,template_folder='../templates',static_folder='../static') # Blueprint app.register_blueprint (user_bp) return app

The code is simple: import create_app directly into the view file and call create_app ().

In this way, the app.py file is associated with the blueprint package.

Note: the above file name, blueprint name, and view function name can all be arbitrary.

So what happens to the view function of our blueprint?

Take the above view view function file as an example, the code content is as follows:

From flask import Blueprint, request, render_template, redirectfrom apps.user.model import User user_bp= Blueprint ('user',__name__) # Blueprint object @ user_bp.route (' /') def hello (): return 'Hello'

Compared to the previous view function written in app.py, change the previous @ app.route () to the blueprint object name .route (), and the rest is almost the same.

At this point, the study on "how to use the Python Flask framework" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report