I'm new to Django and I'm following the guide at https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django. So I thought I would follow along but not entirely copy the code.
I must have made some basic mistake, but I can't figure it out. I read many threads describing similar problems but none solved mine.
When I try to access any page other than index.html I get this Error message:
TemplateDoesNotExist at /catalog/livros/
catalog/livro_list.html
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\Gledyson\PROJECTS\Websites\Library\templates\catalog\livro_list.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Gledyson\PROJECTS\Websites\Library\myvenv\lib\site-packages\django\contrib\admin\templates\catalog\livro_list.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Gledyson\PROJECTS\Websites\Library\myvenv\lib\site-packages\django\contrib\auth\templates\catalog\livro_list.html (Source does not exist)
Django is trying to find my templates in 'templates/catalog/' instead of 'templates/'. I tried moving my templates to 'library/catalog/templates/catalog/' and it works. But I can't manage to make it find my template in 'library/templates'.
My project tree looks somewhat like this:
Library/
|
-- catalog/
| |
| -- static/, admin.py, apps.py, models.py, tests.py, urls.py, views.py
|
-- locallibrary/
| |
| -- settings.py, urls.py, wsgi.py
-- myvenv/
|
-- templates/
|
-- base.html, index.html, livro_detail.html, livro_list.html
My locallibrary/settings.py is:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'EDITED'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'catalog',
]
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 = 'locallibrary.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 = 'locallibrary.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'pt-br'
TIME_ZONE = 'America/Campo_Grande'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
My catalog/urls.py:
from django.contrib import admin
from django.urls import path, include, re_path
from . import views
urlpatterns = [
path('', views.index, name='index'),
# path('lista_de_livros/', views.lista_de_livros, name="lista_de_livros"),
path('livros/', views.ListaDeLivros.as_view(), name="livros"),
re_path(r'^livro/(?P<pk>\d+)$', views.DetalhesDoLivro.as_view(), name="detalhe-livro"),
]
My catalog/views.py:
from django.shortcuts import render, get_object_or_404, redirect
from .models import Gênero, Idioma, Livro, LivroInstância, Autor
from django.views import generic
# Create your views here.
def index(request):
num_livros = Livro.objects.all().count()
num_instâncias = LivroInstância.objects.all().count()
num_instâncias_disponíveis = LivroInstância.objects.filter(estado__exact='d').count() #Livros disponíveis (estado == 'd')
num_autores = Autor.objects.all().count()
num_gêneros = Gênero.objects.all().count()
num_livros_maionese = Livro.objects.filter(título__icontains='maionese').count()
context = {
'num_livros': num_livros,
'num_instâncias': num_instâncias,
'num_instâncias_disponíveis': num_instâncias_disponíveis,
'num_autores': num_autores,
'num_gêneros': num_gêneros,
'num_livros_maionese': num_livros_maionese,
}
return render(request, 'index.html', context=context)
# def lista_de_livros(request):
# lista_de_livros = Livro.objects.all()
# return render(request, 'lista_de_livros.html', {'lista_de_livros' : lista_de_livros})
class ListaDeLivros(generic.ListView):
model = Livro
class DetalhesDoLivro(generic.DetailView):
model = Livro
I've been trying to find my mistake for the past few hours but nothing works other than just accepting that the templates will be in 'catalog/templates/catalog/' without knowing why.
# You need to create 'catalog' folder in your template folder. And keep your 'livro_list.html' template inside that folder. Or else you can define your template location in your template_name variable of your class 'ListaDeLivros'
class ListaDeLivros(generic.ListView):
template_name = 'livro_list.html' # you can define your path here
model = Livro