Weekly letter pythonprogramming.email
Hi,
To get the curated list of awesome python articles from all over the Internet, please subscribe with pythonprogramming.email. This is specifically suitable for beginners.
Advertise with us
templates tag   0   4975
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 custom_template_tags.py

Paste the below code in it.

from django import template
register = template.Library()

@register.simple_tag
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   0   4975

Related Articles:
Django Template Fiddle Launched !!!!
Django template fiddle, Experimenting with Django templates, Playing with Django template, Django fiddle, template fiddle, djangotemplatefiddle.com,...
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...

0 thoughts on 'How To Set A Variable In Django Template'
Leave a comment:


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

SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap