Search code examples
djangodjango-templates

How to assign a value to a variable in a Django template?


e.g., given this template:

<html>
<div>Hello {{name}}!</div>
</html>

While testing it, it would be useful to define the value of the variable without touching the python code that invokes this template. So I'm looking for something like this

{% set name="World" %}     
<html>
<div>Hello {{name}}!</div>
</html>

Does something like this exist in Django?


Solution

  • You can use the with template tag.

    {% with name="World" %}     
    <html>
    <div>Hello {{name}}!</div>
    </html>
    {% endwith %}