fix noreversematch error in django

How to Fix NoReverseMatch Error in Django

Sometimes you may encounter NoReverseMatch Error while using Django-based websites & applications. This is because Django is unable to find URL that matches the url_name specified in your website templates. In this article, we will look at the various causes for this error and how to fix them.


What is NoReverseMatch Error in Django

When you mention a URL in template within {% url … %} tag then Django tries to find that URL in urls.py whose url_name parameter matches the value you have mentioned in your template. When is it unable to find a URL that matches your specified url_name, it returns NoReverseMatch Error. Basically, you get this error when Django is unable to find a Reverse Match for the URL name you have mentioned in your template.


How to Fix NoReverseMatch Error in Django

Let us say you have the following configuration in urls.py.

from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
    #...
    path('articles/<int:year>/', views.year_archive, name='news-year-archive'),
    #...
]


Here are some common reasons for NoReverseMatch Error in Django.


1. Using wrong URL name in template

The most common reason why you get this error is because you are using wrong URL name. For example, in the anchor tag below, url name is news-year-archives instead of news-year-archive as defined in urls.py above.

<a href="{% url 'news-year-archive' 2021 %}">2021 Archive</a>


2. Incorrect Commented HTML code

Please note, that Django parses the template tags and qualifiers inside commented HTML code also. For example, even if you comment the above mentioned anchor tag, you will still get the error.

<!--<a href="{% url 'news-year-archive' 2021 %}">2021 Archive</a>-->

So please check your commented HTML code as well to make sure that it is correct.


3. Incorrect Arguments & Keywords

Make sure that you pass the correct number of URL arguments in your template, as you have defined it in urls.py. For example, our URL defined above requires 1 argument. If you do not pass any argument you will get an error. Here is an example of URL that will give error.

<a href="{% url 'news-year-archive' %}">2021 Archive</a>

Please note, according to Django, a URL is a combination of URL string and arguments. So you need to pass the right number of arguments/keywords in your template to match URLs in urls.py file correctly.


4. Not including an app’s urls.py in your project

If you have a Django app test and if you do not include its URLs in your project’s urls.py then Django will not be able to find its URLs when you refer to them in your template. Here is an example of an incorrect configuration without your app’s urls.py included.

from django.urls import include, path
from django.contrib import admin

urlpatterns = [
    path('admin/', admin.site.urls),
]

Here is the correct configuration.

from django.urls import include, path
from django.contrib import admin

urlpatterns = [
    path('admin/', admin.site.urls),
    path('test/', include('test.urls', namespace="test")),
]


5. Incorrect Regular Expressions in URL definition

The URL arguments are defined with the help of regular expressions in Django. If you have defined a URL to accept numerical argument in urls.py file and you pass an alphanumeric argument in your template then it will give a NoReverseMatch error. Here is an example of URL that accepts only numeric product IDs as its arguments.

re_path(r'product/(?:product-(?P<product_number>\d+)/)?$', product)

The following URL reference will give an error.

<a href="{% url 'product' abc %}">Product ABC</a>


6. Namespace Mismatch

If two or more of your applications use the same URL name, you can differentiate them with the help of namespaces. Once you have added a namespace for URL, you can call it using namespace:url_name syntax. Here is an example to call news-year-archive in test namespace.

<a href="{% url 'test:news-year-archive' 2012 %}">2012 Archive</a>

If you use wrong namespace for your URL in your template, then you may get NoReverseMatch error. This is because two URLs with same name and different namespaces can have different number of arguments/keywords.

In this article, we have looked at the various reasons as to why you may get NoReverseMatch Error in Django and how to fix them in each case.

Also read :

How to Convert PDF to Image/JPG in Ubuntu
How to Redirect with Query String in Apache
How to Check if Cookie is Set in Apache
How to Redirect Apache based on Cookie
How to Redirect Based on Hostname in Apache

2 thoughts on “How to Fix NoReverseMatch Error in Django

Leave a Reply

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