Phython getting issue with urls path

Question:

I’m learning pyhton django, and I created a file nosotros.html inside of templates/paginas:

enter image description here

and on views.py my I have:

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

def inicio(request):
    return HttpResponse("<h1> Bienvenido </h1>")
def nosotros(request):
    return render(request, "paginas/nosotros.html")
def libros(request):
    return render(request, "libros/index.html")

and on my urls.py I have:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.inicio, name="inicio"),
    path('nosotros', views.nosotros, name="nosotros"),
    path('libros', views.libros, name="libros"),
]

so if I try to go to http://127.0.0.1:8000/nosotros or http://127.0.0.1:8000/libros I got TemplateDoesNotExist at /libros or TemplateDoesNotExist at /nosotros

what’s wrong??? or what I’m doing wrong?

the setting.py

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',
            ],
        },
    },
]

this file setting.py is on CRUDMySQL, currently I never write or updated, so it’s intact

Asked By: alex

||

Answers:

You need to include the path to your template directory in the DIRS list:

LIBRERIA_DIR = BASE_DIR / 'libreria'

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

I’m confused about the structure of your project folder, since you have your settings.py in the base directory of your project. The actual path you need to use might be different than the one I provided.

Just make sure the directory you put in DIRS is the absolute path to your template folder.

Answered By: Lord Elrond