Develop personal blogs with flask (5) Four types of response in--flask _redirect

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.