contrast

Flask compared to Django

First of all, we should compare the characteristics of the two frames

Django:

Django-admin allows you to quickly create a project directory, manage.py. Orm is an abstraction layer database, similar to iOS Model, Android entity, and Java Dto Admin

Flask:

Routing, other need to expand the package to complete

Flask-sqlalchemy Flask-migrate management migration database; Flask E-mail Mail; Flask – WTF form; Flask-script Insert script; Flask-login authenticates the user status; Flask-restful is a tool for developing REST apis. Flask-bootstrap integrated front end Twitter Bootstrap framework; Flask-moment localization date and time;

contrast

Flask: Django is powerful, too powerful for large business projects: Simple, flexible, Easy to do, easy to do, easy to do

Flask Flask

The installation

pip3 install Flask

The basic use

from flask import Flask # import framework
app = Flask(__name__) Initialize the Flask
@app.route('/') # add route
     def index(): # route call method
        return 'Hello Word' # the return value
 if __name__ == '__main__':
     app.run(host='0.0.0.0', port=5011, debug=True,) # start Flask
Copy the code

The console prints after successful startup

 * Serving Flask app "server" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5011/ (Press CTRL+C to quit)
Copy the code

In normal cases, app.run() host is the IP address. If this parameter is not set, the browser needs to access 127.0.0.1:5011 to start the program. When set to 0.0.0.0, access the local IP address and port number in the browser. The same applies to the server and development device

Port indicates the port number. If this parameter is not set, the default port number is 5000. If ali cloud server is used, do not forget to release the port

Debug Indicates whether to enable the debug mode

Why I need to enable DEBUG mode:

  1. If the DEBUG mode is enabled, if an exception is thrown in the code, the specific error message and the specific error code location are displayed on the browser page. Easy for developers to debug.

  2. If DEBUG mode is enabled, flask will automatically re-log the entire site by pressing Command + S for any subsequent changes in Python code. No need to manually click to rerun.

Add routes to other files

1. Call

In the development, each module has its own division. When I started the project, I put all the routes in the APP file, resulting in too much file code, which was not convenient to read, so I wanted to write routes in modules

For example, if you have a user module, create a new user.py file and add the following methods:

def login():
     return 'Login successful'
 
 def add_user_routes(app):
 
     app.add_url_rule('/user/login', view_func=login)
Copy the code

Import it in app.py and use it

from login import add_user_routes

add_user_routes(app)
Copy the code

2.

There are roughly three steps to using Blueprint

  1. Create a blueprint object
  2. Operate on this blueprint object to register routes
  3. Register the blueprint object on the application object

You first create the blueprint object in user.py and register the child routes. The code is as follows:

Create a blueprint object
from flask import Blueprint
# two required parameters 'user' blueprint name; The module or package in which the '__name__' blueprint resides, usually in the '__name__' variable
user_blue = Blueprint("user",__name__)

#2 Register a route
#@app.route('/user/login') change to the following code but the URL is '/user/login' to access the 'login()' function method
@index_blue.route('/login')
def login():
    return 'Login successful'
Copy the code

Then register the route object on the application object by adding the following to app.py:

#3. Register the blueprint object on the application object
from user import *
app.register_blueprint(user_blue)
Copy the code

The common parameters of registered routes are as follows

Static_folde accesses a static file. Use /static_login/*** to access a static file in the static_login directory

index_blue = Blueprint("login",__name__,static_folder='static_login')
Copy the code

Static_url_path Use static_URl_path to change the route of a static directory. The access path is /lib/***

index_blue = Blueprint("login",__name__,static_folder='static_login',static_url_path='/lib')
Copy the code

Template_folder Sets the template directory

index_blue = Blueprint('login',__name__,template_folder='my_templates')
Copy the code