AlexW
AlexW

Reputation: 2587

django template url - append variable to string

Im trying to add a variable to the end of a string and am getting issues with reverse match, from what I gather the below should work

{% for type in type_links %}
<li><a href="{% url 'sites:site_list' 'all_'|add:type.id %}">{{ type.subnet_type }}</a></li>
{% endfor %}

I should have a url that has "all_1" for example in it.

however I am getting a reverse url error

Reverse for 'site_list' with arguments '('',)' not found. 1 pattern(s) tried: ['sites/site_list/(?P<display_filter>[\\w\\-]+)$']

is this correct way to add a variable to end the of the string?

EDIT: url pattern, the pattern works as I have tested it manually before trying to create the URLs dyanmically

url(r'^site_list/(?P<display_filter>[\w\-]+)$', views.site_list, name='site_list'),

Thanks

Upvotes: 0

Views: 499

Answers (1)

itzMEonTV
itzMEonTV

Reputation: 20359

You can do

{% for type in type_links %}
<li>
    {% with type.id|stringformat:"s" as str_obj_id %}
      {% with 'all_'|add:str_obj_id as extra_param %}
        <a href="{% url 'sites:site_list' extra_param %}">{{ type.subnet_type }}</a>
      {% endwith %}
   {% endwith %} 
</li>
{% endfor %}

Upvotes: 1

Related Questions