Django raises NoReverseMatch when building a URL
django.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.
Fix it fast
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)))"2Match what you see, then jump to its fix
- 1
Reverse 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
NoReverseMatch 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
The 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
The 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
- 1
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
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.py2Fix
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
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.py2Fix
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
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.py2Fix
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
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:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
Airflow SSHOperator stops a long script with SSH command timed out
airflow.exceptions.AirflowException: SSH command timed out
SSHOperator waits only 10 seconds for a command by default.
3 causes, each with a fixBack end
Python asyncio: Task was destroyed but it is pending
RuntimeWarning: coroutine '...' was never awaited
A task was still unfinished when Python threw it away.
4 causes, each with a fixBack end
Celery in a Django project does not connect to Redis or ignores the broker URL
kombu.exceptions.InconsistencyError: ... _kombu.binding.celery
Celery talks to Redis through a client library and reads its broker URL from your Django settings.
4 causes, each with a fixBack end
Django form POST returns 403 Forbidden: CSRF verification failed
CSRF verification failed. Request aborted.
Django rejects an unsafe request when the CSRF cookie or the form token is missing or wrong, or when the Origin or Referer check fails.
5 causes, each with a fixBack end
Django cannot import corsheaders (django-cors-headers)
ImportError: No module named corsheaders
The pip package is django-cors-headers, but the module Django imports is corsheaders.
3 causes, each with a fixBack 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