This space is available for advertisement. Please contact us if interested.
Advertise with us
templates tag   1   47514
How to set a variable in Django template

We can set the value of a variable in the Django template using with tag. 

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

This will output the below content.

Hello pythoncircle

One downside of this approach is that we have to write the lines where we are accessing the variable inside with and endwith block.

Using with is useful when using a costly variable multiple times. For example, you are printing the count of rows in a database table then it would be better to set the value of row count in a variable instead of getting it every time from the database.

{% with total=business.employees.count %}
{{ total }} employee{{ total|pluralize }}
{% endwith %}

We can rewrite the above with tag as below as well.

{% with business.employees.count as total %}
{{ total }} employee{{ total|pluralize }}
{% endwith %}

We can set multiple variables in one go as below.

{% with var1="hello" var2="pythoncirlce" %}
{{ var1 }} {{var2}}
{% endwith %}

Another approach to declare variables in the template is by using custom template tags.

Create a custom template tag files named as

Paste the below code in it.

from django import template
register = template.Library()

def setvar(val=None):
return val

Now inside the HTML template use this custom template tag setvar to define a new variable.

{% load custom_template_tags %}

{% if is_login %}
{% setvar "Save" as action %}
{% else %}
{% setvar "Compile" as action %}
{% endif %}

Would you like to {{action}} this code?

Using the custom template tag, we don't have to write everything inside with block.

templates tag   1   47514

Related Articles:
Django Template Fiddle Launched !!!!
Django template fiddle, Experimenting with Django templates, Playing with Django template, Django fiddle, template fiddle,,...
Solving Django Error: TemplateDoesNotExist at /app_name/
How to solve TemplateDoesNotExist error in Django projects, reason for TemplateDoesNotExist error in Django, fixing TemplateDoesNotExist error in Django...
Creating custom template tags in Django
Custom template tags in Django, creating new template tags in Django, Step by step guide to create and use custom template tags in Django, how to create custom template tags in Django, how to use custom template tag in Django,...
How to display flash messages in Django templates
flash messages in Django template, one-time notifications in Django template, messages framework Django, displaying success message in Django, error message display in Django...

1 thought on 'How To Set A Variable In Django Template'
Mike Mike :
Thank you a lot). The most understandable explaining after couple hours of search

Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

DigitalOcean Referral Badge

Get a .COM for just $5.98!

© 2021-2022 Python Circle   Contact   Sponsor   Archive   Sitemap   Partner Sites: PythonSnippets.Dev  99Dev.Tools