在jinja2模板中创建Flask应用程序的链接 [英] Creating link to an url of Flask app in jinja2 template

查看:1038
本文介绍了在jinja2模板中创建Flask应用程序的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @ post_blueprint.route('/ post /< ; int:year> /< int:month> /< title>)
def get_post(year,month,title):
#我的代码


要显示最后的10个条目,我有以下的观点:

  @ post_blueprint.route('/ posts /')
def get_all_posts():
#我的代码
返回render_template('p.html',posts = posts)

现在,当我显示最后10个帖子时,我想将帖子的标题转换为超链接。
目前,我必须在我的jinja模板中做到以下几点:

 < a href =/交/ {{年}} / {{月}} / {{TITLE}}> {{TITLE}}< / A> 

有没有办法避免硬编码的url?

url_for 这个功能用于创建Flask这样的URL:

  url_for('view_name',** arguments)


$ b $我已经尝试过寻找一个,但我,我不能找到它。

解决方案

在这里问两个问题,但我会拍一些...



对于发布网址,您可以这样做:

 < a href ={{url_for('post_blueprint.get_post',year = year,month = month,title = title)}}> 
{{title}}
< / a>

为了处理静态文件,我强烈建议使用像 Flask-Assets ,但要用香草瓶来做:

  {{url_for('static',filename ='[filenameofstaticfile]')}} 

如果您想了解更多信息,我强烈建议您阅读。 http://flask.pocoo.org/docs/quickstart/#static-files http://flask.pocoo.org/docs/quickstart/#url-建筑



使用kwargs编辑

如果你想使用 url_for
$ b

  {{url_for('post_blueprint.get_post',** post)}} 
code>

您必须将您的视图更改为如下所示:



<$ p $
def get_all_posts():
models = database_call_of_some_kind#这是假设你使用某种模型
的职位
posts.append(dict(year = model.year,month = model.month,title = model.title))
return render_template('p热媒那么你的模板代码可能看起来像这样:$ / $ p







  {在帖子中发帖%} 
{{post ['title']}}
< / a>
{%endfor%}

此时我会在模型上创建一个方法所以你不必把它变成一个字典,但这么做取决于你: - )。


In my Flask app, I have a view which displays a post

@post_blueprint.route('/post/<int:year>/<int:month>/<title>')
def get_post(year,month,title):
    # My code

To display the last 10 entries, I have following view:

@post_blueprint.route('/posts/')
def get_all_posts():
    # My code
    return render_template('p.html',posts=posts)

Now when I display the last 10 posts, I want to convert the title of a post into a hyperlink. Currently I have to do the following in my jinja template to achieve this:

<a href="/post/{{year}}/{{month}}/{{title}}">{{title}}</a>

Is there any way to avoid hard coding the url?

Like url_for function which is used to create Flask urls like this:

url_for('view_name',**arguments)

I have tried searching for one but I,m not able to find it.

解决方案

I feel like you're asking two questions here but I'll take a shot...

For the posting url you'd do this:

<a href="{{ url_for('post_blueprint.get_post', year=year, month=month, title=title)}}">
    {{ title }}
</a>

To handle static files I'd highly suggest using an asset manager like Flask-Assets, but to do it with vanilla flask you do:

{{ url_for('static', filename='[filenameofstaticfile]') }}

If you'd like more information I highly suggest you read. http://flask.pocoo.org/docs/quickstart/#static-files and http://flask.pocoo.org/docs/quickstart/#url-building

Edit for using kwargs:

Just thought I'd be more thorough...

If you'd like to use url_for like this:

{{ url_for('post_blueprint.get_post', **post) }}

You have to change your view to something like this:

@post_blueprint.route('/posts/')
def get_all_posts():
    models = database_call_of_some_kind # This is assuming you use some kind of model
    posts = []
    for model in models:
        posts.append(dict(year=model.year, month=model.month, title=model.title))
    return render_template('p.html', posts=posts)

Then your template code can look like this:

{% for post in posts %}
    <a href="{{ url_for('post_blueprint.get_post', **post) }}">
        {{ post['title'] }}
    </a>
{% endfor %}

At this point I would actually create a method on the model so you don't have to turn it into a dict, but going that far is up to you :-).

这篇关于在jinja2模板中创建Flask应用程序的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆