簡體   English   中英

如何在 django 模板文件中指定一個變量

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM