Simple inheritance issue with Django templates

2024/11/15 15:21:59

just getting started in Django, and I have some problems with the inheritances. It just seems that the loop for doesn't work when inheriting other template. Here's my code in base.html:

<!DOCTYPE html>
<html lang="es">
<head><title>{% block title %}Titulo del proyecto web{% endblock %}</title>
</head>
<body><div id="header"><h1>Título del proyecto web</h1></div><div id="content">{% block content %}{% endblock %}</div>
</body>
</html>

So here in the index.html the objective is to show the for loop and also the 'header' div of base. Index.html is this:

{% extends "base.html" %}{% block title %}Questions{% endblock %}{% block content %}
{% for pregunta in preguntas %}<h3>{{ pregunta }} ?</h3><br/>
{% endfor %}
{% endblock %}

I've checked the code several times. If I quit the inheritance the loop works fine, but I don't know why it doesn't work when extending to base.html.

When I run the server page it just appears a blank page. Help would be highly appreciate. Thank you very much.

EDIT: Here it is my template directories structure:

Main Project/Templates/ and inside Templates folder there's the base.html and a 'preguntasyrespuestas' folder which is the app name.

And inside 'preguntasyrespuestas' folder there is the index.html template. But it automatically creates a 'base.html' also inside this folder (?) I just delete it.

And the views.py code is that shown here:

from django.http import HttpResponse,Http404
from preguntasyrespuestas.models import Pregunta
from django.shortcuts import get_object_or_404, render_to_responsedef index(request):preguntas = Pregunta.objects.all()return render_to_response('preguntasyrespuestas/index.html',{'preguntas': preguntas})def pregunta_detalle(request, pregunta_id):pregunta = get_object_or_404(Pregunta, pk=pregunta_id)return render_to_response('preguntasyrespuestas/pregunta_detalle.html',{'pregunta': pregunta})

Here's the settings.py template var:

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

So, must both files (base.html and index.html) be in the Templates directory (not inside the app directory inside templates)? I've tried it and still happens the same (output a blank page), if not an error while trying to combine files locations (between theses two folders).

Answer

In your app the template folder structure must be something like:

|- preguntasyrespuestas # your app folder|- templates-base.html|- preguntasyrespuestas-index.html-pregunta_detalle.html....

Templates directory must be in your app folder and inside it must be another folder with the name of your app and inside this must be the template files.

EDIT

If your templates are in the app template folder you should change DIRS to an empty list: DIRS:[]

 TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [], # change here: put an empty list'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',],},},
] 
https://en.xdnf.cn/q/119404.html

Related Q&A

Replacing values in a list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Azure Release Pipeline - Environment variables on python script

Lately Ive been requested to run a python script on my Azure Release Pipeline. This script needs some environment variables for being executed, as Ive seen that in the build pipeline, the task include …

Problem with python prepared stmt parameter passing

File C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\cursor.py, line 1149, in execute elif len(self._prepared[parameters]) != len(params): TypeError: object of ty…

list of lists to list of tuples without loops or list comprehensions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How can I merge CSV rows that have the same value in the first cell?

This is the file: https://drive.google.com/file/d/0B5v-nJeoVouHc25wTGdqaDV1WW8/view?usp=sharingAs you can see, there are duplicates in the first column, but if I were to combine the duplicate rows, no…

i usually get this error : ValueError: invalid literal for int() with base 10

I have loaded a csv file and as i try to print it i get this error Traceback (most recent call last):File "C:\Users\FSTC\Downloads\spaceproject\main.py", line 389, in <module>world_data…

How to Draw a triangle shape in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

DataFrame from list of string dicts with array() values

So I have a list where each entry looks something like this: "{A: array([1]), B: array([2]), C: array([3])}"I am trying to get a dataframe that looks like thisA B C 0 1 2 3 1 4 …

Need Help Making Buttons to perform for loops when you input a number

I am trying to make a button in Maya using Python that when you type in a number the for loop would loop for that many times. For example, I would put 5 in the box so the for loop would loop 5 times re…

Combining multiple conditional expressions in a list comprehension

I utf-8 encode characters like \u2013 before inserting them into SQLite.When I pull them out with a SELECT, they are back in their unencoded form, so I need to re-encode them if I want to do anything w…