302临时重定向(带参重定向)

302临时重定向(带参重定向)

    浏览器第一次访问服务器,服务器会给浏览器一个新的地址,让服务器重新请求。

from flask import Flask, redirect

app = Flask(__name__)


@app.route('/')
def index():
    return 'hello flask,测试302临时重定向!'


@app.route('/cx/<name>/<age>')
def test(name, age):
    return '我的名字是{},我今年{}'.format(name, age)


@app.route('/test_redirect')
# 302临时重定向视图函数
def test_redirect():
    # redirect
    return redirect('/cx/帅B/18')


if __name__ == "__main__":
    app.run(host='127.0.0.1', port=5001, debug=True)

这里需要思考一个问题,如果重定向视图函数路由地址改变后,所有定向到此路由地址的函数都会404报错。

因此引入  url_for

原文地址:https://www.cnblogs.com/cxstudypython/p/12492680.html