【问题标题】:How to use "{" character in django template如何在 django 模板中使用“{”字符
【发布时间】:2023-03-31 21:20:02
【问题描述】:

我知道我们使用 {{ }} 个字符作为变量,但我想在 {} 个字符之间显示我的数据

我想这样展示 11 月{12}。所以我做的是:

<ul>
    {% for month in archive %}
<li><a href="?{{month.date|date:"Y-m"}}">{{month.date|date:"F Y"}}{{{month.count}}}</a></li>
    {% endfor %}
</ul>

但是我遇到了这样的错误:

无法解析余数:来自“{month.count”的“{month.count”

如何在模板中使用 { 字符?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:
    <ul>
        {% for month in archive %}
    <li><a href="?{{month.date|date:"Y-m"}}">{{month.date|date:"F Y"}}&#123;{{month.count}}&#125;</a></li>
        {% endfor %}
    </ul>
    

    【讨论】:

    • 在这种情况下模板是否理解 month.count ?我想,只有在 {{ }} django 才能解释变量..
    【解决方案2】:

    {} 渲染为b_sb_e 到您的模板并执行以下操作:

    <ul>
    {% for month in archive %}
    <li><a href="?{{month.date|date:"Y-m"}}">{{month.date|date:"F Y"}} {{b_s}} {{month.count}}      {{b_e}}</a></li>
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2012-11-18
      • 1970-01-01
      • 2015-09-07
      • 2011-05-22
      • 2017-06-15
      • 2021-04-16
      • 1970-01-01
      • 2016-02-25
      相关资源
      最近更新 更多