The third chapter URL and view

 

Page jump and redirection:

Redirect divided into permanent and temporary redirect redirect on a page that is reflected in the operation of the browser will automatically jump from one page to another page. For example, a user accesses a page requires permission, but the user is currently not logged in, so we should give him redirected to the login page.
· Permanent Redirect:

http status code is 301, more for the old URL was abandoned to go to a new Web site to ensure that the user's access, the most classic is the Jingdong site, you enter www.jingoong.con time, will be redirected to www. jd.com, because jngdong.con this site has been abandoned, it was changed 30.con, so in this case should be a permanent redirect.

· Temporary Redirect:

Http status code amount 302 indicates a temporary jump page. For example, access to a web site requires permission, if the current user does not show Gordon, asked to be re-set to the login page, in which case, you should ask the re-use temporary.

In the flask, the redirection is achieved by flask.redirect (location, code = 302) function, 1ocation represents needs to be redirected to the URL, you should speak before mating ur1_for () function used to effect, using the code which represents redirection, i.e. default temporary redirection 302, 301 may be modified to achieve a permanent redirection.

The following is a permanent redirect Jingdong site:

 The following is Taobao to buy baby [has] temporary redirection:

 

 

 

 

 

 Case code is as follows:

from flask import Flask,request,redirect,url_for

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/login/')
def login():
    return '这是登录页面'

@app.route('/profile/')
def profile():
    if request.args.get('name'):
        return 'Personal Center page ' 
    the else :
         return redirect (url_for ( ' the Login ' ))

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/wqbin/p/11828224.html