如何在 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/