Flask中的 redirect 函数与 url_for 方法
发布时间
阅读量:
阅读量
1. 区别
url_for是一个函数,其作用是接收视图函数的名称(以字符串形式提供)作为输入参数,进而生成与该视图函数相对应的URL地址。redirect作为重定向函数,当传入一个URL时,会自动将用户引导至该URL所指向的页面,例如,在函数中使用 return redirect('xxxx/dsn/0') 的方式,页面将会跳转至对应的地址。此时,redirect的功能即为引导浏览器跳转至指定的URL位置。
2、实例
@app.route('/', methods=['GET'])
def index():
return redirect(url_for('api.index'))
@api.route('/sns', methods=["GET"])
@view_decorator("api")
def index():
user = request.user
type_list = TypeHandler.get_all_types()
return render_template('index.html', **locals())
全部评论 (0)
还没有任何评论哟~
