mommomonthewind
mommomonthewind

Reputation: 4640

django2 + python3: TemplateDoesNotExist

I know there are multiple questions on this site about this problem, but I cannot find a solution.

I am using Python 3.6 (anaconda) + django 2.0.2 on Windows 10.

I am following the tutorial: https://docs.djangoproject.com/en/2.0/intro/tutorial03/

Here is my views.py

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

from .models import *


def index(request):
    content = 'abcxyz'

    context = {'content': content}
    return render(request, 'polls/index.html', context)

I created a file index.html in the folder polls\templates\polls

My settings.py:

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    '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 = 'django_site.urls'

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

WSGI_APPLICATION = 'django_site.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

I have a problem of 'TemplateDoesNotExist' - it seems to me that django tries to look for a template in

django.template.loaders.app_directories.Loader: /mnt/e/workspace/capec-processing/code/django_site/polls/templates/polls/templates/polls/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user_name/anaconda3/envs/capec/lib/python3.6/site-packages/django/contrib/admin/templates/polls/templates/polls/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user_name/anaconda3/envs/capec/lib/python3.6/site-packages/django/contrib/auth/templates/polls/templates/polls/index.html (Source does not exist)

I am not sure what did I do wrong, because I followed the tutorial on the django website.

Could you suggest me a hint?

Update

Here is the structure of the my root directory (called django_site):

django_site
--django_site
----settings.py
--polls
----templates
------polls
--------index.html
----views.py
--db.sqlite3
--manage.py

Upvotes: 1

Views: 1160

Answers (4)

AnotherLongUsername
AnotherLongUsername

Reputation: 541

All you need to do is add the name of your app, i.e. polls, to the list of installed apps in the settings.py file and reload the URL to the app index page. I don't know why the Django tutorial still does not include this step as it trips everyone up, every time.

settings.py

# Application definition

INSTALLED_APPS = [
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'polls',
]

Upvotes: 0

Rohan Baddi
Rohan Baddi

Reputation: 479

In your setting.py you need to add this

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls'   # You need to add this too. This should be same as your app name.
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],  # Add this to your settings file
        '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',
            ],
        },
    },
]

And your template folder should be here

myproject/
 |-- myproject/
 |    |-- polls/
 |    |-- myproject/
 |    |-- templates/     <-- here!
 |    |    |-- polls/
 |    |    |    |-- index.html
 |    |    |-- base.html
 |    |    +-- home.html
 |    +-- manage.py
 +-- venv/

Upvotes: 3

Exprator
Exprator

Reputation: 27523

change your order of the installed apps in your settings

INSTALLED_APPS = [

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls.apps.PollsConfig',
]

Upvotes: 1

Sumeet Kumar
Sumeet Kumar

Reputation: 1019

change value of DIRS under TEMPLATES to this to tell django to look for templates in your app directory.

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

and then you can either put templates parallel to manage.py or under specific app directory.

+app
-+__init__.py
-+templates
---+polls
-----+index.html

Upvotes: 1

Related Questions