Python 获取Flask函数的路由列表

Python 获取Flask函数的路由列表,python,twitter-bootstrap,dynamic,flask,Python,Twitter Bootstrap,Dynamic,Flask,我正在使用Flask和Bootstrap3,我想得到一个函数生成的所有URL的列表,这样我就可以链接到Bootstrap主题中的所有URL e、 g: 以下是我的功能: @cache.cached(timeout=86400) @app.route('/programme/<prog_id>') def programme(prog_id): daily_bands= my_location + "/static/data/bandsdaily/" + prog_id +

我正在使用Flask和Bootstrap3,我想得到一个函数生成的所有URL的列表,这样我就可以链接到Bootstrap主题中的所有URL

e、 g:

以下是我的功能:

@cache.cached(timeout=86400)
@app.route('/programme/<prog_id>')
def programme(prog_id):
    daily_bands= my_location + "/static/data/bandsdaily/"  + prog_id  + ".json"
    event_details = []
    with open(daily_bands) as f:
            for line in f:
                data = json.loads(line)
            event_details.append(data)
    return render_template('index.html', data=event_details) 
@cache.cached(超时=86400)
@应用程序路线(“/Program/”)
def程序(程序id):
daily_bands=my_location+“/static/data/bandsday/“+prog_id+”.json”
事件详细信息=[]
开放式(每日_波段)为f:
对于f中的行:
data=json.loads(第行)
事件\u详细信息。追加(数据)
返回渲染模板('index.html',data=event\u details)

我尝试放置
prog\u id
变量并使用
render\u template
传递它,但它不起作用,我尝试使用
url\u for()
,但我认为后者用于其他目的。

您确实需要在此处使用
url\u for()
;它将为您生成各种
/program/
URL。假设您的
banday
目录中有一系列
prog_id.json
文件,您想链接到这里

您需要获得所有可能的
prog\u id
值的列表,并使用
url\u for()
对每个值进行处理:

{% for prog_id in prog_ids %}
    {{ url_for('programme', prog_id=prog_id) }}
{% endfor %}
并将您的a
prog_id
作为列表传递到模板:

from flask import abort, render_template
import os.path


@cache.cached(timeout=86400)
@app.route('/programme/<prog_id>')
def programme(prog_id):
    path = os.path.join(my_location, "static/data/bandsdaily/")
    prog_ids = [os.path.splitext(filename)[0] for filename in os.listdir(path)]
    if prog_id not in prog_ids:
        # no such file, return a not-found status
        abort(404)

    daily_bands = os.path.join(my_location, prog_id  + ".json")
    with open(daily_bands) as f:
        event_details = [json.loads(l) for l in f]

    return render_template('index.html', data=event_details, prog_ids=prog_ids)
从flask导入中止,呈现\u模板
导入操作系统路径
@cache.cached(超时=86400)
@应用程序路线(“/Program/”)
def程序(程序id):
path=os.path.join(我的位置,“static/data/bandsdaly/”)
prog_id=[os.path.splitext(文件名)[0]表示os.listdir(路径)中的文件名]
如果程序id不在程序id中:
#没有此类文件,返回未找到状态
中止(404)
daily_bands=os.path.join(我的位置,prog_id+“.json”)
开放式(每日_波段)为f:
event_details=[json.loads(l)for l in f]
返回渲染模板('index.html',data=event\u details,prog\u id=prog\u id)

如果
prog\u id
文件不存在,此版本的视图还会返回一个
404未找到
状态。

谢谢,它非常快速、详细并且非常有用。我添加/修改了这段代码:
prog\u id=[os.path.splitext(filename)[0]用于os.listdir(path)中的文件名,如果os.path.isfile(os.path.join(path,filename))]
prog\u id.sort(key=lambda x:datetime.strtime(x,'%d%m%Y'))
from flask import abort, render_template
import os.path


@cache.cached(timeout=86400)
@app.route('/programme/<prog_id>')
def programme(prog_id):
    path = os.path.join(my_location, "static/data/bandsdaily/")
    prog_ids = [os.path.splitext(filename)[0] for filename in os.listdir(path)]
    if prog_id not in prog_ids:
        # no such file, return a not-found status
        abort(404)

    daily_bands = os.path.join(my_location, prog_id  + ".json")
    with open(daily_bands) as f:
        event_details = [json.loads(l) for l in f]

    return render_template('index.html', data=event_details, prog_ids=prog_ids)