在我的Django模板中,我需要将名称url的值分配给with块中的变量,以便我可以在多个位置使用它.
我需要实现这样的目标:
{% for tag in post.tags.all %}
{% with tagabs={%url showtag tag%} %}
<li><a href="{{tagabs}}">#{{tag}}</a></li>
{% endwith %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
但显然这不起作用,最终会出现解析错误.上面的示例是一个简单的场景,我可以使用{%url showtag tag%}而不是{{tagabs}}并删除with块.但在我的场景中,tagabs值我需要在几个地方和if语句中使用它进行比较.
谢谢您的帮助.
Dar*_*win 40
如果功能在核心中,为什么要创建新的模板标记/过滤器?
查看以下示例:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#url
{% url 'path.to.view' arg arg2 as the_url %}
<a href="{{ the_url }}">I'm linking to {{ the_url }}</a>
Run Code Online (Sandbox Code Playgroud)
和
{% url 'path.to.view' as the_url %}
{% if the_url %}
<a href="{{ the_url }}">Link to optional stuff</a>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9388 次 |
| 最近记录: |