4

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?

Chris Tang
  • 567
  • 7
  • 18
K str
  • 55
  • 1
  • 1
  • 4
  • I don't see `BASE_DIR` setting in your settings.py. The error `django.template.loaders.filesystem.Loader: /home/kostis/PycharmProjects/secvot/templates/registration/login.html (Source does not exist)` make me think that django is looking the wrong dir. – Giacomo Catenazzi Mar 03 '18 at 13:46
  • But the directory you mentioned is the correct directory. – K str Mar 03 '18 at 14:29

2 Answers2

10
'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 [].
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I agree with what you're saying, but if you look at the error page you'll see this: 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) So Django looks at the correct path where my template is but still doesn't load it. why? – K str Mar 03 '18 at 13:59
  • You’re not reading the error message or my answer closely enough. Your template directory isn’t at that path, it’s at `/home/kostis/PycharmProjects/secvot/secvot/templates/registration/login.html`. – Alasdair Mar 03 '18 at 14:56
  • Omg. You're right. I totally missed that! Thanks a lot! – K str Mar 03 '18 at 15:44
0

Its a spelling mistake !

in my case the error was

Exception Type: TemplateDoesNotExist

Exception Value: blog/blog_form.html

and the weird part is i din't even created this blog_form.html file.

what was that silly mistake then?

i mentioned template_name as tempplate_name (see the spellings) in the views.py file.

it tooked me more than 30 minutes to find this mistake because most people have a habit of reading quickly (instead of reading letter by letter). Also i couldn't find that blog_form.html file (which don't even exist and most of the time wasted beacuse of this file). So i suggest you to go back and find that spelling mistake. It worked for me i hope it will work for you as well beacuse i know how frustrating it is.

Community
  • 1
  • 1
Yash Marmat
  • 1,077
  • 10
  • 18