djangourlconf

Django - NoReverseMatch Error when using named URL with parameter


I'm trying to build this link using a named URL with a parameter:

http://127.0.0.1:8000/wakemeup/admin/list/colegio

The parameter is the string after .../list/ (in this case "colegio"). I have a print() statement in the admin_list view, which shows the parameter is getting extracted correctly. However, when I try to build the URL:

<a href="{% url 'wakemeup:admin_list' list_type=colegio %}">Colegios</a>

I get this error:

NoReverseMatch at /wakemeup/admin/list/colegio
Reverse for 'admin_list' with keyword arguments '{'list_type': ''}' not found. 1 pattern(s) tried: ['wakemeup/admin/list/(?P<list_type>\\w+)$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/wakemeup/admin/list/colegio
Django Version: 2.0.1
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'admin_list' with keyword arguments '{'list_type': ''}' not found. 1 pattern(s) tried: ['wakemeup/admin/list/(?P<list_type>\\w+)$']

views.py

def admin_list(request, list_type):
    print(list_type)
    return index(request)

urls.py

url(r'^admin/list/(?P<list_type>\w+)$', views.admin_list, name="admin_list"),

I also tried using an unnamed parameter, but that did not work.


Solution

  • To confirm the answer following your comment, remember to correctly format your template url kwargs as strings;

    {% url 'wakemeup:admin_list' list_type='colegio' %}
    

    For further reading the docs on URLs are here; https://docs.djangoproject.com/en/2.0/topics/http/urls/