John Mutuma
John Mutuma

Reputation: 3600

Using url in Django Templates

I have the following django 1.9 endpoint:

url(r'^(?P<pk>\d+)/members/(?P<status>active|deactivated)?$',
    gym.GymUserListView.as_view(),
    name='user-list')

Using the above, a valid url would be like like /1/members/active or /1/members/deactivated

I am trying use the url in one of my templates like:

<button type="button" class="btn btn-outline-warning">
  <a href="{% url 'user-list' pk=gym.id status='deactivated' %}"/>View Inactive</a>
</button>

This throws the following error:

django.core.urlresolvers.NoReverseMatch: Reverse for 'user-list' with arguments '()' and keyword arguments '{'pk': 1, 'status': 'deactivated'}' not found.
0 pattern(s) tried: [ ]

What could be wrong?

Upvotes: 0

Views: 584

Answers (2)

Alasdair
Alasdair

Reputation: 308779

It looks like you have forgotten to include the namespace. The URL tag should be something like:

{% url 'users:user-list' pk=gym.id status='deactivated' %}

Upvotes: 1

Jamie Counsell
Jamie Counsell

Reputation: 8103

This could be a few things.

  • Double check the syntax for that regex (I have not done so). Maybe remove the or pattern for the status parameter and replace it with something generic for now to eliminate that as a potential cause.
  • You can also try removing the second param entirely and see if you can get the URL generated by pk only - this will tell you for sure if the issue lies with the pattern or the configuration of your urls
  • Depending on your app layout, you may need to use the namespace in your templatetag: {% url 'myapp:user-list' pk=gym.id status='deactivated' %}
  • Is the app where the urls.py is for this case in your INSTALLED_APPS?

Upvotes: 2

Related Questions