neurix
neurix

Reputation: 4316

Django: Reverse not found > {% url %} in template

This problem seems simple and it is described multiple times in SO, but I still can't figure out why it isn't working in my case.

So, I have a url declared in urls.py

urlpatterns = patterns('',
    url(r'^(?P<country>[-\w]+)/$', CountryListView.as_view(), name='list_by_country'),)

and in my template I'm calling the url

<a href="{% url 'list_by_country' country.country__name %}" >{{ country.country__name }}</a>

However, I am getting the error message that the url could not be reversed

Reverse for 'list_by_country' with arguments '(u'United Kingdom',)' and keyword arguments '{}' not found

What is causing the reverse error? Are spaces in the argument maybe not allowed?


Upvotes: 1

Views: 2059

Answers (1)

Derek Kwok
Derek Kwok

Reputation: 13058

The problem is that "United Kingdom" doesn't match the regex [-\w]+. If you also want to match spaces, you should change your regex to [-\w\ ]+. Example:

url(r'^(?P<country>[-\w\ ]+)/$', CountryListView.as_view(), name='list_by_country')

You can also choose to match \s instead of just a single space character.

Upvotes: 5

Related Questions