Swan Mougnoz Swan Mougnoz - 22 days ago 6
Python Question

Unpack dictionary as template tag arguments

Is there a way to use a dictionary as named arguments in a template tag in Django?

Assuming I have a dictionary like :

my_dict = {
'param1': 'value1',
'param2': 'value2'
}


I know that can do for example :

a_function(**my_dict)


But can I do something like :

{% url 'my_app.viewname' **my_dict %}


The url tag is actually what I'm trying to use here, maybe is there an other way to achieve this?

Answer

Django template language is not Python, so no, there's no argument unpacking etc. You can eventually write you own custom url tag taking a dict as argument, but that's about it.