Python 在金贾2号铸造钢绞线
我想强制转换一个通过url传递给模板的int,但它表示没有定义Python 在金贾2号铸造钢绞线,python,jinja2,Python,Jinja2,我想强制转换一个通过url传递给模板的int,但它表示没有定义str函数 我该怎么做 这是我的密码: {% extends "base.html" %} {% block content %} {% for post in posts %} {% set year = post.date.year %} {% set month = post.date.month %} {% set day = post.date.day %} {% set p =
str
函数
我该怎么做
这是我的密码:
{% extends "base.html" %}
{% block content %}
{% for post in posts %}
{% set year = post.date.year %}
{% set month = post.date.month %}
{% set day = post.date.day %}
{% set p = str(year) + '/' + str(month) + '/' + str(day) + '/' + post.slug %}
<h3>
<a href="{{ url_for('get_post', ID=p) }}">
{{ post.title }}
</a>
</h3>
<p>{{ post.content }}</p>
{% else: %}
There's nothing here, move along.
{% endfor %}
{% endblock %}
{%extends“base.html”%}
{%block content%}
{posts%%中的post为%s}
{%set year=post.date.year%}
{%set month=post.date.month%}
{%set day=post.date.day%}
{%set p=str(年)+'/'+str(月)+'/'+str(日)+'/'+post.slug%}
{{post.content}}
{%else:%}
这里什么都没有,走吧。
{%endfor%}
{%endblock%}
您可以使用加入
:
{% set p = (year, month, day, post.slug)|join("/") %}
Jinja2还定义了
~
运算符,它首先自动将参数转换为字符串,作为+
运算符的替代
例如:
{% set p = year ~ '/' ~ month ~ '/' ~ day ~ '/' ~ post.slug %}
请参阅,或者,如果确实要使用
str
,请修改字典。要转换为表达式中的字符串,请使用x | string()
而不是str(x)
string()
是一个值得学习的过滤器示例。这让我大吃一惊。随着我使用它,Jinja2变得越来越棒。