Django:在模板中分配变量

标签 django django-templates

如何在 Django 模板系统中分配变量?

假设 Restaurant 是一个模型:

{% restaurant_id as restaurant.id %}{{ restaurant_id as restaurant.id }} 无效。

最佳答案

您可以使用 with template tag ,并像这样分配一个内部模板变量:

{% with restaurant_id=restaurant.id %}
... use restaurant_id in this template section ...
{% endwith %}

关于Django:在模板中分配变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517933/

相关文章:

python - 自动文本摘要

python - Django 模板不加载

python - 在基于类的 ListView 中选择带有复选框的对象

python django 从命令加载缓存 TfidfVectorizer 并在 View 中使用

python - 外键关系中 drf 序列化器的最佳实践是什么

django - 使用 RadioSelect 小部件自定义 Django 表单

python - 在 Django 中使用 {% url %} 时通用 View NoReverseMatch

python - django if 语句中的括号

python - 如何在 django oscar 的策略中应用可变税?

python - csvkit & django a.k.a. 使用 csvkit 作为模块而不是从命令行