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.
- In this example, the user Jerry’s detailed data URL link is http://127.0.0.1:8000/dept_emp/emp_detail/jerry/13919283928/.
- The above URL contains the below five parts.
- Website root path: http://127.0.0.1:8000/
- Django app context path : dept_emp/
- Employee detail page url : emp_detail/
- User name part : jerry/
- User mobile number part : 13919283928/
- 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 detail page link URL in the employee list page ( 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.
- ‘dept_emp:emp_detail’ : This is the Django-app-name : employee-detail-page-url.
- 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.
- 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.
- 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 %}
- And I also need to add a URL mapping in DjangoHelloWorld / dept_emp / urls.py file like below.
url(r'^emp_detail/(?P<user_name>\s+)/(?P<mobile_number>\d{10,18})/$', views.emp_detail, name='emp_detail'),
- 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.
- 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.
- 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.
- 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'),
- 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.
- 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'),
- 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.
- 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.
- When you meet the NoReverseMatch error, just check your URL mapping pattern carefully.
Thank you so much it helped me very well.
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’]