![](/img/trans.png)
[英]Django: How do I display this variable information in models to my template
[英]How do i specify a variable in side of django template file
所以我試圖在我的 django 模板文件中聲明一個變量
{% with object = "{{object.id
}}" %}
{% for detail in details %}
{% if detail.post.id == {{object}} %}
{{detail.name}}
{% endif %}
{% endfor %}
{% endwith %}
我知道 with 用於這項工作,但是當我運行此代碼時,它向我顯示此錯誤:'with' 預期至少有一個變量賦值
請幫我。 謝謝你
post_id
可能是一個int
,因此您應該將object
指定為{% with object=2 %}
,此外,您不應在模板標簽中使用雙花括號:
{% with object=2 %}
{% for detail in details %}
{% if detail.post_id == object %}
{{ detail.name }}
{% endif %}
{% endfor %}
{% endwith %}
然而,在模板中過濾不是一個好主意,因為這效率不高,並且需要更多的 memory,通常您在視圖中過濾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.