“{%”在 HTML 中有什么作用?

Cha*_*ith 5 python django django-templates

我已经使用官方文档向我的应用程序添加了 Django 消息。它说在我的模板中添加这样的东西:

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

我不知道百分比符号是做什么的,它们不是真正的 HTML,对吗?

Grv*_*agi 6

它是 html 页面中使用的模板引擎语法,当您使用上下文变量呈现视图 html 并返回基本 html 响应时,django 会对其进行编译。

In your case your message will be your context variable and django engine compile it like

如果消息不是 None 则内部部分 html 是可见的。

博士。链接:https : //docs.djangoproject.com/en/1.7/topics/templates/

  • Django 不包括 Jinja。Jinja 是一个单独的项目,它的创建是为了可以独立于 Django 使用 Django 模板语法。 (2认同)