How To Fix Django Url NoReverseMatch Error – Reverse For With Keyword Arguments Not Found

The Django Bootstrap3 Example article tells you how to develop a user, department, and employee management website using Django and bootstrap3. The example website provides a user, department, and employee list page.

Now I want to add a link to the employee name on the employee list page. Then when the user clicks the employee name link, it will display the employee detail data on a new page.

1. How To Produce Django Url NoReverseMatch Error.

  1. In this example, the user Jerry’s detailed data URL link is http://127.0.0.1:8000/dept_emp/emp_detail/jerry/13919283928/.
  2. The above URL contains the below five parts.
  3. Website root path: http://127.0.0.1:8000/
  4. Django app context path : dept_emp/
  5. Employee detail page url : emp_detail/
  6. User name part : jerry/
  7. User mobile number part : 13919283928/
  8. Because one employee is identified uniquely by the combination of username and mobile number, so the employee detail page URL must contain username and mobile number.
  9. To achieve this I need to edit the employee detail page link URL in the employee list page ( DjangoHelloWorld / templates / dept_emp / emp_list.html ).
  10. Please note the <a href=”{% url ‘dept_emp:emp_detail’ user_name=emp.user.username mobile_number=emp.emp_mobile %}”>{{ emp.user.username }}</a> code below, the Django url tag has three parameters after it.
  11. ‘dept_emp:emp_detail’ : This is the Django-app-name : employee-detail-page-url.
  12. user_name=emp.user.username : The emp.user.username value (jerry) will be passed to the backend emp_detail view function’s user_name parameter.
  13. mobile_number=emp.emp_mobile : The emp.emp_mobile value (13919283928) will be passed to the backend emp_detail view function’s mobile_number parameter.
  14. Below is the full source code on the employee list page.
    {% for emp in emp_list %}
          
       <tr>
                          
          <td><input type="checkbox" id="emp_{{ emp.id }}" name="emp_{{ emp.id }}"></td>
         
          <td>{{ emp.id }}</td>
         
          <td>
               <a href="{% url 'dept_emp:emp_detail' user_name=emp.user.username mobile_number=emp.emp_mobile %}">{{ emp.user.username }}</a>
          </td>
          
          <td>{{ emp.get_dept_values }}</td>
          
          <td>{{ emp.emp_mobile }}</td>
                          
          <td>{{ emp.emp_salary }}</td>
                          
          <td>{{ emp.emp_onboard_date }}</td>
                        
       </tr>
                     
    {% endfor %}
  15. And I also need to add a URL mapping in DjangoHelloWorld / dept_emp / urls.py file like below.
    url(r'^emp_detail/(?P&lt;user_name&gt;\s+)/(?P&lt;mobile_number&gt;\d{10,18})/$', views.emp_detail, name='emp_detail'),
  16. From the URL mapping definition, we can see emp_detail URL should contain two parameters, one is passed to views.emp_detail function’s user_name, the other is passed to views.emp_detail function’s mobile_number parameter.
  17. But when I run the code, the page URL http://127.0.0.1:8000/dept_emp/emp_list/ returns the NoReverseMatch error like below.
    NoReverseMatch at /dept_emp/emp_list/
    Reverse for 'emp_detail' with keyword arguments '{'user_name': 'jerry', 'mobile_number': 13919283928}' not found. 1 pattern(s) tried: ['dept_emp/emp_detail/(?P<user_name>\\s+)/(?P<mobile_number>\\d{10,18})/$']
    

2. How To Fix Django Url NoReverseMatch Error.

  1. The above error confused me some time, but finally, I find it is a stupid error, it is as simple as the error message said. The URL match pattern is not correct. Below are the steps of how to fix it.
  2.  First, make your URL pattern as simple as possible. I change
    url(r'^emp_detail/(?P<user_name>\s+)/(?P<mobile_number>\d{10,18})/$', views.emp_detail, name='emp_detail'),

    to

    url(r'^emp_detail/(?P<user_name>.*)/(?P<mobile_number>.*)/$', views.emp_detail, name='emp_detail'),
  3. Then I find the URL matching rule takes effect, so this means the original user_name or mobile_number‘s regular expression pattern is not correct.
  4. Then I restore the mobile_number regular expression pattern as original like below, I find it works also.
    url(r'^emp_detail/(?P<user_name>.*)/(?P<mobile_number>\d{10,18})/$', views.emp_detail, name='emp_detail'),
  5. Now, I am sure the user_name‘s regular expression pattern \s+ is not what I want, after search google, I find \s+ means any white space, and what I want is \w+ ( any characters and digits ). Please refer to https://docs.python.org/3/howto/regex.html.
  6. Then I change the URL pattern to below and the URL mapping worked as expected.
    url(r'^emp_detail/(?P<user_name>\w+)/(?P<mobile_number>\d{10,18})/$', views.emp_detail, name='emp_detail'),

3. Conclusion.

  1. When you meet the NoReverseMatch error, just check your URL mapping pattern carefully.

2 thoughts on “How To Fix Django Url NoReverseMatch Error – Reverse For With Keyword Arguments Not Found”

  1. Hi, I am new to django… i am trying to create an to-do list app…

    urls.py:
    ———–
    from django.conf.urls import url
    from . import views
    app_name =”todo_list”
    urlpatterns = [
    url(r’^$’,views.home, name=”home”),
    url(r’delete’,views.delete,name=”delete”)
    ]

    views.py file
    ——————-
    def delete(request,list_id):
    item = List.objects.get(pk=list_id)
    item.delete()
    messages.success(request,(“Item Has Been Deleted”))
    return redirect(‘home’)

    html file
    ————-
    {% if all_items %}

    {% for things in all_items %}

    {{ things.item }}
    {{ things.completed }}

    {% endfor %}

    {% endif %}

    when i run the server, i am getting NoReverseMatch error…

    NoReverseMatch at /
    Reverse for ‘delete’ with arguments ‘(1,)’ not found. 1 pattern(s) tried: [‘$delete’]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.