TemplateDoesNotExist at /accounts/login/

Question:

I have checked the answers here, here, here, and here, and though I am getting the same error apparently I have a different root cause since those answers do not work for me.

Here’s the problem: I am using Django’s LoginView, overriding the template name, but it only sometimes works. If I get to the login screen from the navbar, it works great, but the same url when gotten to from a different method throws a template does not exist error. My URLs file:

from django.urls import path
from django.contrib.auth import views as auth_views

from . import views

app_name = "accounts"

urlpatterns = [
    path(
        "login",
        auth_views.LoginView.as_view(template_name="accounts/login.html"),
        name="login",
    ),
    path("logout", auth_views.LogoutView.as_view(), name="logout"),
    path("signup", views.SignUp.as_view(), name="signup"),
]

I have a nav item for the login screen, and it works great. The relevant section in the template:

{% else %}
<li class="nav-item"><a href="{% url 'groups:all' %}" class="nav-link">Groups</a></li>
<li class="nav-item"><a class="nav-link" href="{% url 'accounts:login' %}">Login</a></li>
<li class="nav-item"><a class="nav-link" href="{% url 'accounts:signup' %}">Sign Up</a></li>
{% endif %}

If someone clicks the login link in the navbar, it takes you to http://127.0.0.1:8000/accounts/login and works great.

BUT: I have another section of code where you need to be logged in for a link to work, and if you’re not it redirects you to the login screen. The login URL looks good to me: http://127.0.0.1:8000/accounts/login/?next=/groups/join/test-group-1, but this time the login is met with this error instead of the login screen:

TemplateDoesNotExist at /accounts/login/
registration/login.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/accounts/login/?next=/groups/join/test-group-1
Django Version: 4.1.4
Exception Type: TemplateDoesNotExist
Exception Value:    
registration/login.html
Exception Location: /opt/homebrew/anaconda3/envs/py311django/lib/python3.11/site-packages/django/template/loader.py, line 47, in select_template
Raised during:  django.contrib.auth.views.LoginView
Python Executable:  /opt/homebrew/anaconda3/envs/py311django/bin/python
Python Version: 3.11.0
Python Path:    
['/Users/brendenmillstein/Dropbox '
 '(Personal)/BSM_Personal/Coding/Udemy/full_stack_django_tutorial/BSM_materials/python/social_network/simplesocial',
 '/opt/homebrew/anaconda3/envs/py311django/lib/python311.zip',
 '/opt/homebrew/anaconda3/envs/py311django/lib/python3.11',
 '/opt/homebrew/anaconda3/envs/py311django/lib/python3.11/lib-dynload',
 '/opt/homebrew/anaconda3/envs/py311django/lib/python3.11/site-packages']
Server time:    Sun, 05 Feb 2023 06:01:34 +0000

I already added all the apps to the INSTALLED_APPS list in settings,py, as well as added

BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIR = Path.joinpath(BASE_DIR, "templates")

as well as added the TEMPLATE_DIR to the TEMPLATES list:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [TEMPLATE_DIR],

The fact that login works when I get to it from the navbar, but doesn’t work other times is confusing me.

What am I missing? I don’t understand why the same URL is working if you get to it by clicking on the Nav, but not working if you get to it by clicking on a link that you need to be logged in to use. Is something wrong in the ?next= portion of the URL? I thought that shouldn’t matter.

Send help, I’m going nuts over here trying to make this work.

Asked By: BLimitless

||

Answers:

Maybe Django is looking for the wrong template name, try checking it, might be it is template_name="registration/login.html" in LoginView.

Also check template_name in that view which has the route /groups/join/test-group-1.

Note: I’d recommend you add a trailing / at the end of every route so in urls.py:

from django.urls import path
from django.contrib.auth import views as auth_views

from . import views

app_name = "accounts"

urlpatterns = [
    path(
        "login/",
        auth_views.LoginView.as_view(template_name="accounts/login.html"),
        name="login",
    ),
    path("logout/", auth_views.LogoutView.as_view(), name="logout"),
    path("signup/", views.SignUp.as_view(), name="signup"),
]
Answered By: Sunderam Dubey