Django: Assigning variables in template

前端 未结 2 459
一生所求
一生所求 2021-02-02 05:37

How can I assign a variable inside the django templating system ?

Assuming Restaurant is a Model:

{% restaurant_id as restaurant.id %}

相关标签:
2条回答
  • 2021-02-02 06:14

    You could use the with template tag, and assign an internal template variable like this:

    {% with restaurant_id=restaurant.id %}
    ... use restaurant_id in this template section ...
    {% endwith %}
    
    0 讨论(0)
  • 2021-02-02 06:22

    Note: You can use filters within the "with" template tag:

    foo is {{ foo }}
    {% with bar=foo|slice'6:9' %}
      bar is {{ bar }}
    {% endwith %}
    
    0 讨论(0)
提交回复
热议问题