{"id":12,"date":"2014-02-17T02:43:04","date_gmt":"2014-02-16T18:43:04","guid":{"rendered":"http:\/\/vinta.ws\/code\/?p=12"},"modified":"2026-03-17T01:12:17","modified_gmt":"2026-03-16T17:12:17","slug":"how-to-set-a-variable-in-django-template","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/how-to-set-a-variable-in-django-template.html","title":{"rendered":"How to Set a Variable in Django Templates?"},"content":{"rendered":"<p>Creating a custom template tag for assigning variables in Django templates.<\/p>\n<h2>in app\/templatetags\/common.py<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-py\">from django import template\nregister = template.Library()\n\nclass SetVarNode(template.Node):\n\n    def __init__(self, var_name, var_value):\n        self.var_name = var_name\n        self.var_value = var_value\n\n    def render(self, context):\n        try:\n            value = template.Variable(self.var_value).resolve(context)\n        except template.VariableDoesNotExist:\n            value = \"\"\n        context[self.var_name] = value\n\n        return u\"\"\n\n@register.tag(name='set')\ndef set_var(parser, token):\n    \"\"\"\n    {% set some_var = '123' %}\n    \"\"\"\n    parts = token.split_contents()\n    if len(parts) &lt; 4:\n        raise template.TemplateSyntaxError(\"'set' tag must be of the form: {% set &lt;var_name&gt; = &lt;var_value&gt; %}\")\n\n    return SetVarNode(parts[1], parts[3])<\/code><\/pre>\n<h2>in app\/templates\/xxx.html<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-html\">{% load common %}\n\n{% if work and is_public_work %}\n    {% ifequal link_class 'info_box pull-right' %}\n        {% set ga_label = 'card' %}\n    {% else %}\n        {% set ga_label = ga_label_detail %}\n    {% endifequal %}\n    &lt;a class=\"{{ ga_label }}\"&gt;123&lt;\/a&gt;\n{% endif %}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Creating a custom template tag for assigning variables in Django template.<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,116],"tags":[13,2],"class_list":["post-12","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-python","category-about-web-development","tag-django","tag-python"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}