"TemplateDoesNotExist" when I want to display datas from my database

Hello,

It is the first time I import datas from my database managed with Wamp. I can not reach to import on Django datas from specific colums of my database. It is about a digital library which I want to display the catalog on the homepage. Here is the error message released on my navigator opened at the locally homepage of my project called “sgbj” :

TemplateDoesNotExist at /
books/books.html
Request Method: 	GET
Request URL: 	http://127.0.0.1:8000/
Django Version: 	5.0.2
Exception Type: 	TemplateDoesNotExist
Exception Value: 	books/books.html
Exception Location: 	C:\Users\my_username\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\template\backends\django.py, line 84, in reraise
Raised during: 	sgbj.views.home_view
Python Executable: C:Users\my_username\AppData\Local\Programs\Python\Python312\python.exe
Python Version: 	3.12.1
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
    This engine did not provide a list of tried templates.
Error during template rendering
In template C:\cygwin64\home\my_username\DJANGO\sgbj\templates\home.html, error at line 7
books/books.html
1 	{% extends 'template_base.html' %}
2 	
3 	{% block titre %}Page d'accueil{% endblock %}
4 	
5 	{% block contenu %}
6 	<h1>Ma bibliothèque</h1>
7 	{% include 'books/books.html' %}
8 	{% endblock %}

In a first time, I checked the path of my application called “books”. The path contained in the urls.py file of my application “books” is this one :

from django.contrib import admin
from django.urls import path
from . import views 
urlpatterns = [
    path('', views.books_home_view, name='home'),
]

I effectively want to display the view of my application “books” on the homepage of my project “sgbj”.
In a second time, I checked my code in the models.py field of my application “books” :

from django.db import models
class Book(models.Model):
    lastname = models.CharField(max_length=100) 
    firstname = models.CharField(max_length=100) 
    title = models.CharField(max_length=200) 
    isbn = models.CharField(max_length=20) 
    def __str__(self):
        return self.title

Then, the views.py field of my app “books” :

from django.shortcuts import render
from books.models import Book
def books_home_view(request):
    books = Book.objects.all()
    return render(request, 'books/books.html', context={'books': books})

And finally, here is the code of the homepage of my “sgbj” project where I included books.html of my app “books” :

{% extends 'template_base.html' %}
{% block titre %}Page d'accueil{% endblock %}
{% block contenu %}
<h1>Ma bibliothèque</h1>
{% include 'books/books.html' %}
{% endblock %}

I was forgetting to specify the templates variables I added in settings.py field of my “sgbj” project :

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['books/books.html', 'C:\\cygwin64\\home\\my_username\\DJANGO\\sgbj\\books\\templates', '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',
            ],
        },
    },
]

I hope I have provided you with sufficient code so that you can easily identify why I am unable to display my library catalog. Thank you in advance…

Relf

‘books/books.html’ is not a directory, it does not belong in the DIRS entry.

You have 'APP_DIRS': True in your setting, which means you do not need to add that to the DIRS setting either.

You should remove both of those entries.

You’re rendering books/books.html. Is this the same books/books.html that you’re including in your home page? What does books/books.html look like?

First, this is a template, not “code”. (This distinction is important in Django)

Second, what file is this that you are showing? (What’s its name and what directory is it in?)

Thank you for taking the time to help me! I am doing my best to improve my Django skills.

Here is the architecture of my Django :

=> Yes, that “books/books.html” is the same one I am including in my homepage. Here is books.html (“title”, “lastname”, “firstname” and “isbn” are 4 column names belonging to the same “document” table) :

{% for book in books %}
<div>
    <h2>{{ book.title }}</h2>
        <p>{{ book.lastname }}</p>
        <p>{{ book.firstname }}</p>
        <p>ISBN : {{ book.isbn }}</p>
<br>
</div>
{% endfor %}

=> The file I am showing is “home.html”. It belongs to the folder “templates” that belongs to the project “sgbj” :

{% extends 'template_base.html' %}
{% block titre %}Page d'accueil{% endblock %}
{% block contenu %}
<h1>Ma bibliothèque</h1>
{% include 'books/books.html' %}
{% endblock %}

The error you’re seeing is being caused by having books.html directly in the templates directory. It needs to be in a directory named books that is inside templates. In other words, it should be sgbj/books/templates/books/books.html

Side issue:

But you’re not rendering home.html in your home view. Your view is trying to directly render books/books.html.

So I added a folder in my “books” application and the path is now : sgbj/books/templates/books/books.html Now I can display my homepage, but without the books view included inside.

  • The following view is the one of my “books” app, contained in the views.py file :
from django.shortcuts import render
from books.models import Book
def books_home_view(request):
    books = Book.objects.all()
    return render(request, 'books/books.html', context={'books': books})
  • The following homepage view is contained in the views.py file of my “sgbj” project :
from django.http import HttpResponse
from django.shortcuts import render
#from .models import document

def home_view(request):
    #documents = document.abjects.all()
    #return HttpResponse('Hello world!')
    return render(request, 'home.html')

def contact_view(request):
    #return HttpResponse('Contactez-nous')
    return render(request, 'contact.html')

Briefly, your home_view view isn’t getting any data to be rendered.

I’m curious - have you worked your way through either (or both) of the Official Django Tutorial and the Django Girls Tutorial?

If you haven’t, you should. They cover the background information you should have to understand what’s happening here.

I am going to read this! Until now I followed tutorials on different youtube channels.