In the previous code, the view function returned a string, and the content of the HTML file returned in the actual development was actually a string. Below is a summary of several ways in which flask can be used as a response. 1, response string.
This has been explained earlier, but it should be noted that in fact, every time a view function responds with a status code. For example, the status code is 200, indicating that the request has been successfully processed. The status code is 400, which indicates that the request is invalid.
@app. Route ('/')
def index (): Return
"Bad Request", 400
2, Response Response object
Using the Make_reponse () function to accept strings and error codes, return a response object that, in this way, can not only successfully process the request, but also set the response further, such as setting cookies, and so on:
From flask import Make_response
@app. Route ('/')
def index ():
response=make_response (' Set cookies! ')
Response.set_cookie (' Hyman ', ' 123 ') return
response
3. Return redirect Type redirect
A redirect type is when you enter a URL and automatically jumps to the address of another URL
From flask Import redirect
@app. Route ('/")
def Index (): Return
redirect (' http://www.baidu.com ')
After entering ' localhost:5000 ' in the browser, jump directly to the Baidu interface
4, return processing error code
Using Abort to return processing errors
From flask import Abort
@app. Route ('/')
def index ():
abort (404)
GitHub Location:
Https://github.com/HymanLiuTS/flaskTs
Clone this item:
Git clonegit@github.com:hymanliuts/flaskts.git
Get the source code for this article:
git checkout FL05