Django error: Reverse for '...' not found. '...' is not a valid view function or pattern name

4.4k views Asked by At

It gives the error: Reverse for 'login' not found. 'login' is not a valid view function or pattern name.

urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('App.urls')),
    path('accounts/', include(('django.contrib.auth.urls', 'django.contrib.auth'), namespace='login')),
    path('accounts/', include('Account.urls')),
] 

index.html:

<a href="{% url 'login' %}"><h3 class="agileinfo_sign">Sign In </h3></a>
3

There are 3 answers

0
Mubashar Javed On BEST ANSWER

Use this:

<a href="{% url 'login:login' %}"><h3 class="agileinfo_sign">Sign In </h3></a>

Hope this small change will solve your problem.

0
Parvana On

In my case, it happened to me when I forgot to add the login_url = 'view_name' field of login_required() decorator.

before:

@login_required()
def checkout_page(request):
    return render(request, 'checkout.html', context)

after:

@login_required(login_url='your_login_view_name')
def checkout_page(request):
    return render(request, 'checkout.html', context)

I hope this can help someone who wants only authenticated users can access the checkout page.

0
Super Kai - Kazuya Ito On

I got the same error below:

django.urls.exceptions.NoReverseMatch: Reverse for '/account/two_factor/setup/' not found. '/account/two_factor/setup/' is not a valid view function or pattern name.

Because I set a normal URL to url tag instead of a URL namespace as shown below:

<a href="{% url '/account/two_factor/setup/' %}">Two factor setup</a>

So, I set a URL namespace to url tag as shown below, then the error was solved:

<a href="{% url '/account/two_factor/setup/' %}">Two factor setup</a>

Or, I set a normal URL to href attribute as shown below, then the error was solved:

<a href="/account/two_factor/setup/">Two factor setup</a>