Django - Reverse for '' not found. '' is not a valid view function or pattern name
When you use the url tag you should use quotes for string literals, for example:
{% url 'products' %}
At the moment product
is treated like a variable and evaluates to ''
in the error message.
- The syntax for specifying url is
{% url namespace:url_name %}
. So, check if you have added theapp_name
in urls.py. - In my case, I had misspelled the url_name. The urls.py had the following content
path('<int:question_id>/', views.detail, name='question_detail')
whereas the index.html file had the following entry<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
. Notice the incorrect name.
I was receiving the same error when not specifying the app
name before pattern name.
In my case:
app-name
: Blog
pattern-name
: post-delete
reverse_lazy('Blog:post-delete')
worked.
Add store name to template like {% url 'app_name:url_name' %}
App_name = store
In urls.py,
path('search', views.searched, name="searched"),
<form action="{% url 'store:searched' %}" method="POST">
Fix urlpatterns
in urls.py file
For example, my app name is "simulator",
My URL pattern for login
and logout
looks like
urlpatterns = [
...
...
url(r'^login/$', simulator.views.login_view, name="login"),
url(r'^logout/$', simulator.views.logout_view, name="logout"),
...
...
]