How to Fix Django's NoReverseMatch Error

A NoReverseMatch error is a common Django error and one that can often be fixed with just one line of code.

NoReverseMatch at /
Reverse for 'index' not found. 'index' is not a valid view function or pattern name.

The cause of the error is likely to be in your template (a HTML file), in urls.py or views.py.

The NoReverseMatch error happens when Django can't find a path in urls.py that matches the provided name.

URLs can be referenced by name in templates, or in a view using the reverse, reverse_lazy, or redirect functions.

Solving the error is a matter of making sure the correct name and the correct arguments are supplied.

What is a URL name?

In urls.py, URL patterns take the following format:

path("filter/<str:status>", views.filter_tasks, name="filter")

This URL has a name of filter. Whenever you need to make reference to a URL, you can use the name instead of having to write out the whole URL.

Django will take the name and look it up in urls.py. If it finds a match, it will return the URL of the match. If it cannot find a match, it will raise a NoReverseMatch error.

Common Causes

1. Typo in urls.py or your template

This particular error has the name of the URL that couldn't be matched:

NoReverseMatch at /
Reverse for 'index' not found. 'index' is not a valid view function or pattern name.

Go to urls.py file and confirm that there is a URL named index:

# todo/urls.py
from django.urls import path
from todo import views

urlpatterns = [
    path("", views.index, name="index"),
]

2. You forgot to put quotations around the name

Consider this HTML snippet:

<a href="{% url filter Status.TODO 123%}">To Do</a>

This will give you the following error:

NoReverseMatch at /
Reverse for '' not found. '' is not a valid view function or pattern name.

You can fix this by putting single quotations around filter, the URL name.

<a href="{% url 'filter' Status.TODO 123%}">To Do</a>

3. You have provided too many or too few arguments

Consider this error:

NoReverseMatch at /
Reverse for 'filter' with arguments '(Task.StatusChoice.TODO, 123)' not found. 2 pattern(s) tried: ['filter/(?P<status>[^/]+)\\Z', 'filter/(?P<status>[^/]+)\\Z']

This is what I provided in my template:

<a href="{% url 'filter' Status.TODO 123%}">To Do</a>

And this is the URL pattern in urls.py:

path("filter/<str:status>", views.filter_tasks, name="filter")

Django has matched the name but my template provided 2 arguments; the URL path only expects one.

Because the number of arguments doesn't match, Django cannot match the URL name, even though there is a URL pattern named "filter".

The URL pattern in this example expects one argument. You can fix the error by removing the extra argument from the template.

4. Your app's URLs haven't been registered

Say you have checked your template and urls.py and there definitely isn't a typo. The link in your template has the correct syntax and the correct number of arguments have been supplied.

The next step is to check the URLs for your app have been imported into the main urls.py.

Django projects are organised by apps. Your site directory (the folder that contains settings.py also has a file called urls.py that contains the index of URLs for the entire project. Each app can have its own urls.py file, but those URLs need to be imported into the main urls.py folder.

If you only have one app, then this won't be the cause of your error. If you have one app and the URLs haven't been imported into urls.py properly then you will get this screen instead of a NoReverseMatch error:

Check your site's main urls.py file.

If you haven't imported all the URLs for your app, then you will need to import include from django.urls and add a path to the URL patterns.

How to include your app's URLs:

For example, my main urls.py file looks like this:

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

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("sample.urls")),
    path("", include("todo.urls")),
]

5. Error in "reverse", "reverse_lazy" or "redirect"

NoReverseMatch errors don't just happen in templates. They can happen anywhere in your code that tries to get a URL from a URL name. They are often used in views.py to redirect the user.

Three common functions that do this are:

  • Reverse

  • Reverse Lazy

  • Redirect

The error could be in the name of the URL or the arguments supplied.

Consider this URL pattern:

path("filter/<str:status>", views.filter_tasks, name="filter")

It has a name of "filter" and accepts one argument- a string called "status".

Because the URL pattern has parameters, reverse("filter") will return a NoReverseMatch error.

Instead, you must provide the "status" argument:

url = reverse("filter", args=["complete"])

The following is also acceptable if you prefer to name your arguments:

url = reverse("filter", kwargs={"status": "complete"})

Conclusion

NoReverseMatch errors occur when you are trying to get the URL from its name.

This error often occurs in templates, when you are trying to add a link to another page, or in views.py when you want to redirect a request to another view.

In both cases, it can be as simple as checking the name against urls.py. If the name matches, then check if the correct number of arguments have been supplied. Providing too many or too few arguments will trigger the error.

If that still hasn't fixed the issue, then check that the URLs of you app have been included in your site's main urls.py (the one in the same folder as settings.py). If the urls.py belonging to an app hasn't been included, then Django won't be able to find it.

Understand More

If you are new to Django, then you may find some examples helpful.

This article from my To Do list tutorial goes through setting up URLs for the first time.

This article from the same tutorial series, gives an example of how to implement URLs with multiple arguments.