Create the first flask project today, the main app file code is as follows:
#Import flask This class from the Flask framework fromFlaskImportFlask#Initializes a Flask object#flasks ()#need to pass a parameter __name__#1. Convenient flask framework to find resources#2. Convenient flask plug-in, such as Flask-sqlalchemy error, to find out where the problem isApp = Flask (__name__)#@app. Route is an adorner#@ begins with, and above the function, the description is the adorner, the function of this adorner, is to make a map of the URL with the view#127.0.0.1:5000/, go to request Hello_world this function, and then return the result to the browser@app. Route ('/')defHello_world ():return 'Hello world!'#If the current file is running as an ingress program, run App.run ()if __name__=='__main__': #App.run () #start an application server to accept requests from users #While True: #Listen ()App.run ()
Places to be aware of:
# # # of the first Flask program explained:
1. When you create a project for the first time, add the flask virtual environment. When adding a virtual environment, be sure to choose the executable file for Python, such as the path on my computer is D:\Postgraduate\Python\Virtualenv\flask-env\Scripts\python.exe
# # # Set Debug mode
1. In App.run (), pass in a keyword parameter debug,app.run (debug=true), set the current project to debug mode
Two main functions of 2.debug mode:
* When there is a problem with the program, you can see the error message and the error location on the page.
* As long as the ' Python ' file in the project has been modified, the program will load automatically and does not need to be restarted manually
# # # Use config file
1. Create a new ' config.py ' file
2. Import this file into the main app file and add the configuration to ' app ', the sample code is as follows:
‘‘‘
Import Config
App.config.from_object (config)
‘‘‘
3. There are many other parameters that are placed in this configuration file, such as ' Secret_key ' and ' SQLALCHEMY '
This time the project directory is as follows:
The main app file debug_model.py code is as follows:
fromFlaskImportFlaskImportConfigapp= Flask (__name__) app.config.from_object (config) @app. Route ('/')defHello_world ():return 'I was'if __name__=='__main__': App.run ()
The configuration file config.py code is as follows:
DEBUG = True
Open Project
If debug mode is not set, no debugger will appear.
Flask Web Development Road II