Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I don't understand this TemplateDoesNotExist error

I am very new to Django. Currently following the book django for beginners (https://djangoforbeginners.com/) and all was fine and dandy until chapter 4. I get the following error:

TemplateDoesNotExist at /

home.html, posts/post_list.html

I don’t know where this comes from. Why is Django looking for the posts/post_list.html file? I am not referring to that file anywhere afaik.

I get this error message:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Request Method:     GET
Request URL:    serverurl
Django Version:     2.1
Exception Type:     TemplateDoesNotExist
Exception Value:    

home.html, posts/post_list.html

Exception Location:     /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/template/loader.py in select_template, line 47
Python Executable:  /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/bin/python3
Python Version:     3.10.12
Python Path:    

['/home/tang/LinuxServ/message_board_django',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages']

Server time:    Fri, 13 Oct 2023 10:31:52 +0000

Django tried loading these templates, in this order:

Using engine django:

    django.template.loaders.filesystem.Loader: /home/tang/LinuxServ/message_board_django/templates/home.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/contrib/admin/templates/home.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/contrib/auth/templates/home.html (Source does not exist)

Using engine django:

    django.template.loaders.filesystem.Loader: /home/tang/LinuxServ/message_board_django/templates/posts/post_list.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/contrib/admin/templates/posts/post_list.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/contrib/auth/templates/posts/post_list.html (Source does not exist)

Does it come from my file structure?
screenshot showing project structure

#posts/view.py

from django.views.generic import ListView
from .models import Post




class HomePageView(ListView):
    model = Post
    template_name = 'home.html'
<!-- templates/home.html -->

<h1>Message board homepage</h1>

<ul>
    {% for post in object_list %}
        <li>{{ post }}</li>
    {% endfor %}
</ul>

settings file:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))




DEBUG = True

ALLOWED_HOSTS = ['*']



INSTALLED_APPS = [
    'posts.apps.PostsConfig',
    '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 = 'mb_project.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',
            ],
        },
    },
]

WSGI_APPLICATION = 'mb_project.wsgi.application'



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


STATIC_URL = '/static/'
/posts/urls.py
from django.urls import path

from .views import HomePageView


urlpatterns = [

    path('', HomePageView.as_view(), name='home')

]
#urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('posts.urls')),
]

>Solution :

Your template directory configuration is incorrect. If you configure just template, django will search for the files in ./templates but the files are in ./mb_project/templates

It should be this way:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading