Site Backend _python+flask.0007.flask construction Jump 301 jump with 302 redirect?

Source: Internet
Author: User

Construction Address:

Description: Flask supports the construction of URLs through view functions and parameters, and future modification URLs can be modified at once, and the default build escapes special characters and Unicode data, which we do not need to process ourselves, and are not only supported in the context but also in the template file.

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public module imports Uuidfrom flask Import Flask, url_for# Description: Import other modules app = Flask (__name__) @app. Route ('/instance/') def IND EX (): Return url_for (' Index ', Uuid=uuid.uuid4 ()) If __name__ = = ' __main__ ': App.run (host= ' 0.0.0.0 ', port=9000, debug= True)

Note: Flask.url_for receives the function name as the first parameter, also receives the variable part of the URL rule, the unknown variable is added to the end of the URL as a query parameter, it should be noted that the URL must be constructed in the context of the request or in the context of the program, as above is constructed in the context of the request, If you want to test in the context of a program in the With App.test_request_context (): code block


Jump Orientation:

Description: Flask.url_for is responsible for constructing the URL, while the flask.redirect is responsible for redirecting, receiving 2 parameters, the first is the URL address, the second parameter is the code status code, jump (301) for the old URL before discarding to the new URL to ensure user access, Represents a permanent redirect, while a redirect (302) represents a temporary transfer of the page, and the Flask.redirect default is 3,020 when redirected

#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman#  51ctobg: http://xmdevops.blog.51cto.com/# purpose:# "" "#  Description:  Import public module from flask  import flask, redirect, request#  Description:  Import other modules App = flask (__name__) @app. Route ( '/<path:page_name>/') @app. Route ('/') Def index (page_name= "):    print "     UserAgent: {}    Method   : {}     GetArgs  : {}    PostArgs : {}      ". Format (        request.headers.get (' user-agent '),         request.method,         request.args,        request.form,    )     return rediRect (' Http://{0}/{1} '. Format (' www.newweb.net ',  page_name),  code=301) if __name__ ==  ' _ _main__ ':     app.run (host= ' 0.0.0.0 ',  port=9000, debug=app.debug)

Note: If an adorner is added to the View function index, indicating that two URL rules are applied to this view function, because all the request data runs are saved in the Request object, you can obtain information by printing the method or property of a Request object. Then 301 redirects to the specified Web page, and finally says a little bit debug=app.debug this is because Flask.config.ConfigAttribute has configured the agent in the app, the default configuration agent currently exists app.debug/ app.testing/app.secret_key/app.session_cookie_name/app.permanent_session_lifetime/app.use_x_sendfile/ App.logger_name



This article is from "@Can up and No BB ..." Blog, be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1866215

Site Backend _python+flask.0007.flask construction Jump 301 jump with 302 redirect?

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.