Notes on Flask in Python,
Here I will first talk about some of the first mistakes app = Flask (_ name _). When I first spelled them, I reported errors. Then I found two '_'
Configuration File
App. config. from_object (_ name _) query configuration items in the current file
App. config. from_envvar ('flaskr _ settings', silent = True) queries the configuration in the file. FLASKR_SETTINGS indicates the environment variable, and silent = True indicates that Flask does not care whether the key value of the environment variable exists.
@ App. route ('/')
Def index ():
Return 'this is Index'
Here, the route modifier binds a function to a URL, @ app. route ('hello'), which means http: // 127.0.0.1/hello
@ App. route ('/user <username> ')
Def show_user (username ):
Return 'My name is % s' % username
In route, <username> represents a parameter. in the address bar, enter http: // 127.0.0.1/user/Sky. The webpage returns My name is Sky.
From flask import url_for
@ App. route ('/login ')
Def index (): return 'this is a new url'
Print url_for ('login ')
Url_for () is used to construct a URL for a specific function. When run, it prints the constructed URL and enters http: // 127.0.0.1/login in the address bar to access the URL.
Why do you need to build URLs instead of hard encoding in the template? There are three good reasons:
1. Reverse Construction is generally more descriptive than hard encoding. More importantly, it allows you to modify the URL at a time, instead of looking for URL changes everywhere.
2. The constructed URL can explicitly process special characters and Unicode escaping, so you do not have to deal with these.
3. If your application is not in the URL root directory (for example, in/myapplication but not in/), url_for () will handle it properly for you.
Generate a URL for the static file when placing the static file
Url_for('static', filename}'style.css ')
This file should be stored in the static/style.css file system.
Rendering files
From flask import render_template
@ App. route ('/hello /')
@ App. route ('/hello <name> ')
Def hello (name = None ):
Return render_template('hello.html ', name = name)
The template file hello.html will be searched from templates.
Request object
From flask import request
Request. method = ['get', 'post']
File Upload
Set attributes in HTML FormsEnctype = "multipart/form-data", otherwise the browser will not transmit the file
From flask import request
@ App. route ('/upload', methods = ['get', 'post'])
Def upload_file ():
If request. method = 'post'
F = request. files ['file']
F. save ('/uploads' + secure_filename (f. filename) because the client name may be forged, never believe it and pass it to secure_filename for effect.
COOKIE operation
Read cookie:
Username = request. cookies. get ('username ')
Storage cookie:
From flask import make_response, render_template
Loadc = make_response (render_template ())
Loadc. set_cookie ('usernmae', 'the username ')
Redirection
Error redirection
If an error occurs, define a decorator.
@ App. errorhandler (404) indicates that the file does not exist.
Def page_not_found (error ):
Return render_tamplater ('**. html'), 404 here 404 is called in render_tamplate, telling Flask that the error code for this page is 404
To be continued...