Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: TemplateDoesNotExist at /accounts/login/

Tags:

django

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:

enter image description here

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:

enter image description here

But I'm seeing:

TemplateDoesNotExist at /accounts/login/ registration/login.html

enter image description here

like image 378
Omar Gonzales Avatar asked Oct 13 '18 02:10

Omar Gonzales


3 Answers

you must not create your template in project setting folder.

you can create template directory in your project root directory like this:

gallito (Django project folder)
   |_gallito
   |_templates (new)
                |_registration

you can see template directory is in your project root beside your app directory. not in your settings directory.

also you can move your template directory into your project template directory

like image 177
vorujack Avatar answered Sep 26 '22 15:09

vorujack


For me worked adding:

'DIRS': [os.path.join(BASE_DIR, 'templates')],

Take care to delete browser cache.

like image 36
user13493631 Avatar answered Sep 26 '22 15:09

user13493631


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',
]
like image 37
Nadhem Jbeli Avatar answered Sep 26 '22 15:09

Nadhem Jbeli