'Django: TemplateDoesNotExist at /accounts/login/
I'm following this tutorial from Mozilla.org,
Setting up your authentication views
And I'm having problems in this part:
The next step is to create a registration directory on the search path and then add the login.html file.
Because I've created that directory and the template but it appears that is not been recognize by my app.
Structure:
Note: Your folder structure should now look like the below:
locallibrary (Django project folder)
|_catalog
|_locallibrary
|_templates (new)
|_registration
My Structure:
And then for this part:
make these directories visible to the template loader
I've:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
#'DIRS': [os.path.join(BASE_DIR, 'templates')],
'DIRS': ['./templates',],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Project url.pý:
from django.contrib import admin
from django.urls import path, include
from main_app import views
urlpatterns = [
path('accounts/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
path('', include('main_app.urls')),
]
For my login.html template I've:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico">
<title>Jumbotron Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="../../dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="jumbotron.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" rel="stylesheet">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" rel="stylesheet">
<link href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" rel="stylesheet"/>
<link href="{% static 'main_app/style.css' %}" rel="stylesheet">
</head>
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<div>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</div>
<div>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</div>
<div>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</div>
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endblock %}
</html>
According to the tutorial I should navigate to:
http://127.0.0.1:8000/accounts/login/
and see this:
But I'm seeing:
TemplateDoesNotExist at /accounts/login/ registration/login.html
Solution 1:[1]
For me worked adding:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Take care to delete browser cache.
Solution 2:[2]
add your app's name gallito to INSTALLED_APPS inside the file settings.py if your code is like this
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
then change it to this
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gallito',
]
Solution 3:[3]
The Django 3.2 documentation introduces a new syntax for building file paths:
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
So the directory listing in TEMPLATES should look like this:
'DIRS': [BASE_DIR / 'templates'],
This works when putting the templates folder as suggested by @vorujack.
Solution 4:[4]
This should fix it.
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Remember:
Django provides no default template for the authentication views. You should create your own templates for the views you want to use. The template context is documented in each view, see All authentication views.
Read this in: https://docs.djangoproject.com/en/3.1/topics/auth/default/#module-django.contrib.auth.views
Solution 5:[5]
in my case (py3.6) it works i have created that tempates file under main project App
'DIRS': ['./templates'],
Solution 6:[6]
check your settings.py file. if the app is not added in the INSTALLED_APPS, add it. LIKE INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts'<--- your app name
]
Solution 7:[7]
Make sure that you have imported your template name or directory correctly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Azametzin |
| Solution 2 | Martin Brisiak |
| Solution 3 | henrikstroem |
| Solution 4 | Ehsan Barkhordar |
| Solution 5 | The Sun |
| Solution 6 | SHREYANK |
| Solution 7 | Nimantha |



