flask中url_for使用endpoint和视图函数名

在flask中,使用url_for 进行路由反转时,需要传递一个endpoint的值,用法如下:

@app.route('/', endpoint='my_index')

def index():

  return 'index page'

@app.route('/hello')

def hello():

  return url_for('.my_index', _external=True)

说明:

1.在url_for 反转时,接受一个endpoint或者函数名为参数(如果是endpoint,endpoint前面需要加个点),返回对应的url地址(可看源码)

2.在前端页面如果使用url_for的时候,也需要加上点,如:{{ url_for('.my_index') }}

3._external=True 如果设置为True,则生成一个绝对路径URL

原文地址:https://www.cnblogs.com/lishuaijiang/p/12454610.html