Search code examples
pythondjangodjango-templatestemplatetags

Django Template: using double curly brace in template tag


What I am trying to do is allow different, but very similar views to use the same template. There is a url reference, where the only thing that differs between them is the second half of the reverse call. Using the example below, I get a NoReverseMatch, which makes sense if the code below is not accepted.

{% url outer:{{suffix}} %}

What is the correct way to achieve this? or do I have sacrifice conciseness, and write out the url?

I am using Django 1.4


Solution

  • You can use 'with' to build url and then pass it to url template tag:

    {% with 'outer:'|add:suffix as link_url %}
        <a href="{% url link_url %}">link</a>
    {% endwith %}
    

    UPD: For django 1.4 to make it work, you should add {% load url from future %}, like this:

    my_template.html:

    {% load url from future %}
    {% url 'outer:'|add:suffix %}
    

    Should print your url correctly. Note, you shouldn't wrap suffix with '', as it's variable.