Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Django raises NoReverseMatch when building a URL

Errordjango.urls.exceptions.NoReverseMatch: Reverse for '' not found. '' is not a valid view function or pattern name.

Short answer

NoReverseMatch means Django looked for a named URL pattern that fits the name and arguments you gave and found none. Check four things in order: the name is quoted and not an empty variable, the namespace prefix matches app_name, the pattern is included in the project URLconf, and the number and names of arguments match the pattern.

  • Applies to Django 5.2, Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the name inside the quotes in the error: an empty name points at a template variable, a real name points at namespace, include or arguments. Then list every URL name Django knows to see whether yours exists.

    python manage.py shell -c "from django.urls import get_resolver; print(sorted(str(k) for k in get_resolver().reverse_dict.keys() if isinstance(k, str)))"
  2. 2Match what you see, then jump to its fix

    1. 1Reverse for '' not found. '' is not a valid view function or pattern name.The name is empty, or a quote is missing in the url tagFix
    2. 2NoReverseMatch for a name that exists in an included app (the same name works inside that app only).The namespace is missing or does not match app_nameFix
    3. 3The name you typed does not appear in the list printed by the first step.The pattern has no name, or it is not in the project URLconfFix
    4. 4The name exists in the list from the first step, but NoReverseMatch is still raised when you pass arguments.The arguments do not fit the patternFix

What does the error look like?

  • django.urls.exceptions.NoReverseMatch: Reverse for '' not found. '' is not a valid view function or pattern name.
  • NoReverseMatch

Cause 1 of 4

The name is empty, or a quote is missing in the url tag

1Check

The first argument of {% url %} may be a quoted literal or a context variable. Without quotes, Django treats the word as a variable. If that variable is not in the context it is empty, and the name Django tries to reverse is ''.

grep -rn "{% url" templates/ */templates/ | grep -v "{% url '" | grep -v '{% url "'

2Fix

Quote literal names. If the name really comes from a variable, make sure the view passes it in the context (also when you render a different template than you meant).

{# wrong: detail is read as a variable #}
<a href="{% url detail post.pk %}">Read</a>
{# right #}
<a href="{% url 'detail' post.pk %}">Read</a>

3Verify

Load the page again. As a quick probe you can use the as form, which does not raise when the name is missing.

{% url 'detail' post.pk as post_url %}{{ post_url }}

Confirmed by docs.djangoproject.com

Did this fix it?

Cause 2 of 4

The namespace is missing or does not match app_name

1Check

When an included URLconf sets app_name (or a namespace in include()), its names are only reachable with the prefix, for example polls:index. Compare the prefix you wrote with app_name in the included urls.py.

grep -rn "app_name\|namespace=" */urls.py */*/urls.py

2Fix

Use the fully qualified name, namespace first, then a colon.

# polls/urls.py
app_name = "polls"
urlpatterns = [path("", views.IndexView.as_view(), name="index")]

# template
<a href="{% url 'polls:index' %}">Polls</a>

# Python
reverse("polls:index")

3Verify

Reverse the name in a shell.

python manage.py shell -c "from django.urls import reverse; print(reverse('polls:index'))"

Confirmed by docs.djangoproject.com

Did this fix it?

Cause 3 of 4

The pattern has no name, or it is not in the project URLconf

1Check

Reversing needs a named pattern. A pattern without name=, a typo in the name, or an app whose urls.py is never passed to include() in the project urls.py is invisible to reverse().

grep -rn "include(" */urls.py

2Fix

Add the name to the path() and include the app's URLconf in the project.

# app/urls.py
path("<int:pk>/", views.DetailView.as_view(), name="detail"),

# project/urls.py
urlpatterns = [path("polls/", include("polls.urls"))]

3Verify

Run the first-step command again and look for the name.

python manage.py shell -c "from django.urls import reverse; print(reverse('polls:detail', args=[1]))"

Confirmed by docs.djangoproject.com

Did this fix it?

Cause 4 of 4

The arguments do not fit the pattern

1Check

reverse() matches the name plus the number of arguments and the names of keyword arguments. An empty value (for example an object with no pk yet), a missing argument, or a path converter that rejects the value (it raises ValueError in to_url) all give NoReverseMatch. Do not mix positional and keyword arguments in one call.

grep -n "detail" */urls.py

2Fix

Pass exactly the arguments the pattern declares, with real values.

# path("<int:pk>/", ..., name="detail")
reverse("polls:detail", args=(obj.pk,))
reverse("polls:detail", kwargs={"pk": obj.pk})

3Verify

Reverse with a real object.

python manage.py shell -c "from django.urls import reverse; print(reverse('polls:detail', kwargs={'pk': 1}))"

Confirmed by docs.djangoproject.com

Did this fix it?

How do I stop it coming back?

  • Prefix URL names with the app name (for example polls-detail) or always use app_name so names do not collide.
  • Write a small test per page that calls reverse() for each named route.
  • Use {% url ... as var %} only for genuinely optional links; for everything else let the error show.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

npm install fails inside a node:alpine Docker build

Docker Node Alpine Image Build Fails on node-gyp

The node alpine images ship almost nothing beyond Node.js, so a native module that node-gyp must compile has no Python, make or C++ compiler.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.