Simple inheritance issue with Django templates
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_response
def 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).
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',
],
},
},
]
链接地址: http://www.djcxy.com/p/90136.html
上一篇: 没有匹配的函数调用错误C ++
下一篇: Django模板的简单继承问题