Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: TemplateDoesNotExist at /login/ (Source Does Not Exist)

I'm trying to get into Django and I'm following tutorials here and there. I'm trying to add a login page to the polling app from the official Django tutorial but even though I'm following this simple tutorial to the letter I get the TemplateDoesNotExist error. From what I can tell though, Django is looking for login.html in the correct directory and gives the message Source doesn't exist.

My file structure:

├── db.sqlite3
├── manage.py
├── secvot
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── templates
│   │   ├── admin
│   │   │   └── base_site.html
│   │   └── registration
│   │       ├── login.html
│   │       └── logout.html
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── voting
    ├── admin.py
    ├── admin.pyc
    ├── apps.py
    ├── apps.pyc
    ├── __init__.py
    ├── __init__.pyc
    ├── migrations
    │   ├── 0001_initial.py
    │   ├── 0001_initial.pyc
    │   ├── 0002_auto_20180301_2354.py
    │   ├── 0002_auto_20180301_2354.pyc
    │   ├── __init__.py
    │   └── __init__.pyc
    ├── models.py
    ├── models.pyc
    ├── static
    │   └── voting
    │       ├── images
    │       │   └── background.gif
    │       └── style.css
    ├── templates
    │   └── voting
    │       ├── detail.html
    │       ├── index.html
    │       └── results.html
    ├── tests.py
    ├── tests.pyc
    ├── urls.py
    ├── urls.pyc
    ├── views.py
    └── views.pyc

settings.py:

INSTALLED_APPS = [
    'voting.apps.VotingConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'secvot.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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',
            ],
        },
    },

secvot/urls.py:

from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^login/', auth_views.login, {'template_name': 'registration/login.html'}, name='login.html'),
    url(r'^logout/', auth_views.logout, name='logout'),
    url(r'^polls/', include('voting.urls')),
    url(r'^admin/', admin.site.urls),
]

login.html:

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <form method="POST">
        {% csrf_token %}
        <p>
            <label>Username</label>
            <input type="text" name="username">
        </p>
        <p>
            <label>Password</label>
            <input type="password" name="password">
        </p>
        <button type="submit">Login</button>
    </form>
</body>
</html>

and part of the error page:

TemplateDoesNotExist at /login/
registration/login.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/login/
Django Version: 1.11
Exception Type: TemplateDoesNotExist
Exception Value:    
registration/login.html
Exception Location: /home/kostis/.virtualenvs/secretvoting/local/lib/python2.7/site-packages/django/template/loader.py in select_template, line 53
Python Executable:  /home/kostis/.virtualenvs/secretvoting/bin/python
Python Version: 2.7.12
Python Path:    
['/home/kostis/PycharmProjects/secvot',
 '/home/kostis/.virtualenvs/secretvoting/lib/python2.7',
 '/home/kostis/.virtualenvs/secretvoting/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/kostis/.virtualenvs/secretvoting/lib/python2.7/lib-tk',
 '/home/kostis/.virtualenvs/secretvoting/lib/python2.7/lib-old',
 '/home/kostis/.virtualenvs/secretvoting/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/kostis/.virtualenvs/secretvoting/local/lib/python2.7/site-packages',
 '/home/kostis/.virtualenvs/secretvoting/lib/python2.7/site-packages']


Template-loader postmortem
Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.filesystem.Loader: /home/kostis/PycharmProjects/secvot/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/kostis/PycharmProjects/secvot/voting/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/kostis/.virtualenvs/secretvoting/local/lib/python2.7/site-packages/django/contrib/admin/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/kostis/.virtualenvs/secretvoting/local/lib/python2.7/site-packages/django/contrib/auth/templates/registration/login.html (Source does not exist)

From what I can tell Django tries to load the correct template, looks into the correct folder but shows the error "Source does not exist" even though the template is there.

Any ideas on what I might be doing wrong?

like image 750
K str Avatar asked Jan 03 '23 17:01

K str


1 Answers

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

This tells Django to look for a templates directory in your project directory (the one containing manage.py. However your templates directory is in your inner project directory (the one containing settings.py.

You could fix it by doing one of the following:

  1. Move the templates directory
  2. Change the DIRS settings to point to the current location of the templates directory.

    'DIRS': [os.path.join(BASE_DIR, 'secvot', 'templates')],
    
  3. Add secvot to your INSTALLED_APPS so that the app_directories loader finds the templates directory. If you do this, you can change DIRS to [].
like image 88
Alasdair Avatar answered Jan 05 '23 08:01

Alasdair