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

The Django Bootstrap3 Example article tell you how to develop a user, department and employee management website using django and bootstrap3. The example website provide user, department and employee list page. Now i want to add a link to the employee name in the employee list page like below picture. Then when user click the employee name link, it will display the employee detail data in a new page.

add link to employee user name in the employee list page

1. How To Produce Django Url NoReverseMatch Error.

From above picture, we can see that the user Jerry’s detail data url link is The url contains five parts.

  1. Website root path :
  2. Django app context path : dept_emp/
  3. Employee detail page url : emp_detail/
  4. User name part : jerry/
  5. User mobile number part : 13919283928/
  6. 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.

To achieve this i need to edit the employee list page ( DjangoHelloWorld / templates / dept_emp / emp_list.html ) like below.

django project app dept_emp source files structure

DjangoHelloWorld / templates / dept_emp / emp_list.html

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.

  1. ‘dept_emp:emp_detail’ : This is the Django-app-name : employee-detail-page-url.
  2. 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.
  3. 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.
{% for emp in emp_list %}
      <td><input type="checkbox" id="emp_{{ }}" name="emp_{{ }}"></td>
      <td>{{ }}</td>
           <a href="{% url 'dept_emp:emp_detail' user_name=emp.user.username mobile_number=emp.emp_mobile %}">{{ emp.user.username }}</a>
      <td>{{ emp.get_dept_values }}</td>
      <td>{{ emp.emp_mobile }}</td>
      <td>{{ emp.emp_salary }}</td>
      <td>{{ emp.emp_onboard_date }}</td>
{% endfor %}

And i also need to add a url mapping in DjangoHelloWorld / dept_emp / file like below. From the url mapping definition, we can see emp_detail url should contain two parameter, one is passed to views.emp_detail function’s user_name, the other is passed to views.emp_detail function’s mobile_number parameter.

url(r'^emp_detail/(?P<user_name>\s+)/(?P<mobile_number>\d{10,18})/$', views.emp_detail, name='emp_detail'),

But when i run the code, i meet 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})/$']

django url match no reverse match error

2. How To Fix Django Url NoReverseMatch Error.

This 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 is the steps of how to fix it.

  1.  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'),


    url(r'^emp_detail/(?P<user_name>.*)/(?P<mobile_number>.*)/$', views.emp_detail, name='emp_detail'),

    Then i find the url matching rule take effect, so this means the original user_name or mobile_number’s regular expression pattern is not correct.

  2. Then i restore the mobile_number regular expression pattern as original like below, i find it work also.
    url(r'^emp_detail/(?P<user_name>.*)/(?P<mobile_number>\d{10,18})/$', views.emp_detail, name='emp_detail'),
  3. 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 charactor and digits ). Please refer Then i change the url pattern to below and finally 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.

When you meet NoReverseMatch error, just check your url mapping pattern carefully.

READ :   How To Debug Django Project With Eclipse And PyDev

Leave a Reply

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.