【发布时间】:2012-06-22 21:46:42
【问题描述】:
在我的 Flask 应用程序中,我有一个显示帖子的视图
@post_blueprint.route('/post/<int:year>/<int:month>/<title>')
def get_post(year,month,title):
# My code
要显示最后 10 个条目,我有以下视图:
@post_blueprint.route('/posts/')
def get_all_posts():
# My code
return render_template('p.html',posts=posts)
现在,当我显示最后 10 个帖子时,我想将帖子的标题转换为超链接。 目前我必须在我的 jinja 模板中执行以下操作才能实现此目的:
<a href="/post/{{year}}/{{month}}/{{title}}">{{title}}</a>
有什么方法可以避免对 url 进行硬编码?
像 url_for 函数,它用于创建这样的 Flask url:
url_for('view_name',**arguments)
我试过找一个,但我找不到。
【问题讨论】:
-
所以你想让 url_for 使用 kwargs?做到这一点的唯一方法是将帖子作为字典列表。我不相信有办法在不首先更改视图中的逻辑的情况下做到这一点。
标签: python flask jinja2 url-for