【问题标题】:why do we not use {{ }} while removing hardcoded urls in django?为什么我们在 django 中删除硬编码的 url 时不使用 {{ }}?
【发布时间】:2017-12-03 22:49:15
【问题描述】:
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

这里我们使用 {{ }} 标签,因为 question.id 是一个变量,但是,

<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

当我们从模板中删除硬编码的 url 时,为什么我们不在 question.id 周围使用 {{ }}?

【问题讨论】:

    标签: django django-templates django-urls


    【解决方案1】:

    {% %} 表示模板标签,其中的任何内容都被视为 python 代码。将其视为一种逃避。而{{ }} 本身会转义变量,以便您可以使用这些值。

    有关更多信息,请参阅文档:https://docs.djangoproject.com/en/dev/topics/templates/#the-django-template-language

    【讨论】:

      猜你喜欢
      • 2018-12-09
      • 2016-02-21
      • 2014-05-21
      • 2013-06-25
      • 2014-10-22
      • 1970-01-01
      • 2016-04-06
      • 2016-01-12
      • 1970-01-01
      相关资源
      最近更新 更多