How to set a variable in Django template
So, you need for some reason to set a variable inside a Django template? As you may have figured by now, Django does not implement such a feature. That is, because the logic of setting the variable must go in the view, not in the template. But there are some rare cases when you actually need this.
The way you can solve this is by using a custom Django template tag.
To do this, you create a file called ‘set_var.py’ in your templatetags forlder that contains the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | from django import template register = template.Library() class SetVarNode(template.Node): def __init__(self, var_name, var_value): self.var_name = var_name self.var_value = var_value def render(self, context): try: value = template.Variable(self.var_value).resolve(context) except template.VariableDoesNotExist: value = "" context[self.var_name] = value return u"" def set_var(parser, token): """ {% set <var_name> = <var_value> %} """ parts = token.split_contents() if len(parts) < 4: raise template.TemplateSyntaxError("'set' tag must be of the form: {% set <var_name> = <var_value> %}") return SetVarNode(parts[1], parts[3]) register.tag('set', set_var) |
Then to use this in your template, you just do the following:
1 2 3 4 5 6 7 8 9 | {% load set_var %}
{% set a = 3 %}
{% set b = some_context_variable %}
{% set c = "some string" %}
Value of a is {{a}}
Value of b is {{b}}
Value of c is {{c}} |
As you can see, you can put in your variables anything that you could normally get inside a Django template: a number, another context variable, a string, etc.
Hope it helped

Thanks you~ it’s a useful way.
Hi
I have been using this today and it does not work in this case:
{% set var = 0 %}
{% for history in page.historyList %}
{% set var = 1 %}
{% endfor %}
{{ var }} # this render 0
I am assuming this is because the ‘context’ passed to ‘render’ is different in both cases, any ideas on how to get around that?
Thanks
I’ll have to check this on Monday, but I believe is due to the case that the tag will set the varable in the current context, and for’s have their own context. Again, I’ll have to check this in Monday.
I expect that is what is going on, I had to work around it. Thanks for the code nonetheless, I used it elsewhere and was able to refactor out hacks I had put in.
Thanks for the tip ! Saved me a lot of frustration
I needed this to make make a nest of unordered lists that could be used by javascript to turn into a ‘tree’ – this helped nicely to determine the level of the tree Thanks !!
Perfect example of why this is needed inside Django templates. Hopefully, they will introduce this feature soon (like in Jinja, if I’m not wrong)
You are welcome!
hi, i tried setting a var inside a if template tag and dosnt work:
{% if imagen.imagen.width >= 400 %}
{% set c = “some string” %}
{% endif %}
{{c}}
this dosnt do anything.
Any ideas about this?
Yes, the variable was set in the context of that if, if I’m not wrong. I’ll try to come up with an update to fix this. I also have to try it myself.
Thanks for the post, very useful. It’s a pity that django doesn’t have support for this out of the box!
how to increment or add with another number SET variable
The tag code will have to be slightly adjusted so that parts[3] is evaluated before is used as a value. If I’ll have time I’ll implement your idea, shouldn’t be hard …
quick dirty fix for context problems, only dealing with the upper-most context.
This can be done by changing any context call by context.dicts[0] :
example :
context[self.var_name] = value
becomes :
context.dicts[0][self.var_name] = value
{% for topic in my_topics %}
{% set already_follow = true %}
{% include “topic_item.html”%}
{% endfor %}
already_follow will not work in topic_item.html
What should I do?
Isn’t the {% with %} tag enough?
https://docs.djangoproject.com/en/1.4/ref/templates/builtins/
Trying this with google app engine, I’ve tried everything, still getting: TemplateSyntaxError: ‘set_var’ is not a valid tag library: Template library set_var not found, tried google.appengine._internal.django.templatetags.set_var
Have you read my reply on StackOverflow ?
Here’s a snippet of my code,
{% if forloop.last %}
{% set flag = 1 %} {%else%} {% set flag = 0%}
{%endif%}
.
.
{% if flag == 1 %}
do this
{% endif %}
It does not check the condition for if. How do I make it work? Sorry if the question is a bit too naive, I’m new to this.
Nevermind I found the solution
hii… can you please share the solution…
Hi, I don’t quite remember how it worked but I believe I used ifequal built in template tag to compare the value of flag. You can look here. Hope it helps.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#ifequal
{% set a = 3 %}
{% set b = 3 %}
{% if a==b %}
print
{% else %}
none
{% endif %}
### why if loop is not working…???