-->

Passing multiple variables to Django Template's Assignment Tags

By: Varghese Chacko 1 year, 10 months ago

Django template tags allows passing multiple variables or values to the assignment tags. Most people knows passing one variable, but not many and we see many questions how to pass it. Most are confused because of the colon separated passing of values to filters. Comma separation wont work either. The Django supports it by putting space as the separator.

Example:

Assume that I want to combine two arrays, one with keys and one with values to a dictionary and assign to a variable for iteration. I can write the function as

@register.assignment_tag()
def make_dictionary(keys_list, keys_list):
    return dict(zip(keys_list, values_list))


This could be used in template as

{% make_dictionary my_keys my_values as my_dict %}

Happy coding !

Let us talk!

We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.