Every Django developer encounters below error at least once in their life for sure.
NoReverseMatch at /url_path/ Reverse for 'url_name' with arguments '()' and keyword arguments '{}' not found. n pattern(s) tried: []
Django is trying to find a URL with name 'url_name' and it is not able to find any pattern match.
Application traverses the urls.py
file from top to bottom and match the pattern. It stops at first successful match.
Arguments '()' and keyword arguments '{}' means that the URL application is searching for have no arguments or keyword arguments in it.
Application tried to match against 'n' patterns.
urls.py
file.
from django.urls import path from . import views app_name = 'polls' urlpatterns = [ #... path('articles/<int:year>/', views.year_archive, name='news-year-archive'), #... ]
In above configuration:
1) Using wrong URL name in template html file.
For example in below template code snippet,
<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>instead of using
news-year-archive
are you using news-year-archives
2) Commented HTML code.
Django tries to parse the template tags in commented HTML code as well while trying to render the template.
If all the url template tags in your template are correct, check the commented part as well.
If there is any error in commented part, remove it.
<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a> <!-- <a href="{% url 'news-year-archives' 2012 %}">2012 Archive</a> -->In the snippet above, application will throw the error for second line as there is no URL with name 'news-year-archives '.
3) Arguments or Keyword arguments
Make sure you are passing the correct number of Arguments or Keyword arguments in URL template tag.
As you can see in urls.py
file, URL with name 'news-year-archive ' expects an argument 'year' in it. If you are not passing the year in url template tag, application will throw the error.
So
<a href="{% url 'news-year-archive' %}">2012 Archive</a>
should be
<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>
Make sure you have included the app urls in project urls.py file.
from django.urls import include, path from django.contrib import admin urlpatterns = [ path('admin/', admin.site.urls), path('app_name/', include('app_name.urls', namespace="app_name")), ]
URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names.
For example app1 have URL name 'contacts', now app2 can also have URL with same name provided we are using namespace while including URLs in project's urls.py file.
In the template snippet inside url template tag, we are using namespace:url_name
syntax
<a href="{% url 'app_name:news-year-archive' 2012 %}">2012 Archive</a>
If you are using any regular pattern in your URLs, make sure it is correct.
In below URL page_number
should consist only digits (\d+).
re_path(r'comments/(?:page-(?P<page_number>\d+)/)?$', comments)
In Django 1.10, the ability to reverse a URL by its python path was removed. The named path should be used instead.
If your application is still throwing this error, comment here and we will help you sail through this.
Host your Django Application for free on PythonAnyWhere. If you want full control of your application and server, you should consider DigitalOcean. Create an account with this link and get $100 credits.