我看官方的文档
url for 是根据函数去创建链接.
我也照着官网的sample去做了一遍,并没有看出什么效果.
百度下也没有问这么小白的问题.遂来求助~
不胜感激.
用于反向路由。
本质是根据函数名反向生成url,使用函数 url_for() 来针对一个特定的函数构建一个 URL。它能够接受函数名作为第一参数,以及一些关键字参数, 每一个关键字参数对应于 URL 规则的变量部分。未知变量部分被插入到 URL 中作为查询参数。
# -*- coding: utf-8 -*-
from flask import Flask,request,url_for
@app.route('/test')
def query_user():
'''
http://127.0.0.1:5000/test?id=123
'''
id = request.args.get('id')
return 'query user:'+id
@app.route('/query_url')
def query_url():
'''
反导出 query_user函数名对应的url地址
'''
return 'query url:'+url_for('query_user')
请求地址http://127.0.0.1:5000/query_url
,之后,打印的是 query url:/test
参考我的博客
看官方文档:http://flask.pocoo.org/docs/0.10/api/#flask.url_for
Generates a URL to the given endpoint with the method provided.
根据给定的endpoint(即函数)来生成url
比如定义了一个路由:
@route('/test_route')
def test():
pass
那么执行
url_for(test)
将会返回一个字符串 '/test_route'