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.
1. How To Produce Django Url NoReverseMatch Error.
From above picture, we can see that the user Jerry’s detail data url link is http://127.0.0.1:8000/dept_emp/emp_detail/jerry/13919283928/. The url contains 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 list page ( DjangoHelloWorld / templates / dept_emp / emp_list.html ) like below.
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.
{% 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. 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})/$']
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.
- 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 take 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 work 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 charactor and digits ). Please refer https://docs.python.org/3/howto/regex.html. 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.
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’]
Thank you so much it helped me very well.