【问题标题】:Simple inheritance issue with Django templatesDjango模板的简单继承问题
【发布时间】:2016-02-01 18:01:54
【问题描述】:

刚开始使用 Django,我在继承方面遇到了一些问题。似乎在继承其他模板时循环不起作用。这是我在 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>

因此,在 index.html 中,目标是显示 for 循环以及 base 的“标题”div。 index.html 是这样的:

{% extends "base.html" %}

{% block title %}Questions{% endblock %}

{% block content %}
{% for pregunta in preguntas %}
    <h3>{{ pregunta }} ?</h3><br/>
{% endfor %}
{% endblock %}

我已经检查了好几次代码。如果我退出继承,循环工作正常,但我不知道为什么它在扩展到 base.html 时不起作用。

当我运行服务器页面时,它只显示一个空白页面。帮助将不胜感激。非常感谢。

编辑:这是我的模板目录结构:

Main Project/Templates/ 和 Templates 文件夹内有 base.html 和一个 'preguntasyrespuestas' 文件夹,它是应用程序的名称。

在“preguntasyrespuestas”文件夹内有 index.html 模板。但它也会在这个文件夹中自动创建一个“base.html”(?)我只是删除它。

views.py 代码如下所示:

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})

这是 settings.py 模板变量:

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

那么,两个文件(base.html 和 index.html)都必须在 Templates 目录中(而不是在模板内的 app 目录中)吗?我已经尝试过了,如果在尝试合并文件位置(在这两个文件夹之间)时没有错误,仍然会发生相同的情况(输出一个空白页)。

【问题讨论】:

  • 你需要展示你的观点。
  • 你必须展示你的模板文件夹结构和内容。
  • Django 不会自动创建 base.html 文件。但是你要去哪一个视图(以及你为什么要发布另一个视图)?
  • 我尝试使用 {{ pregunta.asunto }} (这是它的一个字段),但它仍然没有显示任何结果。问题是,如果没有继承,循环表现得很好,只是现在想不出为什么不。
  • @Jim,显示空白页可能意味着错误来自其他地方。你可能在错误的地方调试?或者也许是浏览器缓存?但要回答您的简单问题,所有模板文件必须在模板LOADERS 可以找到的任何位置提供,这两个文件都可以放置在不同的模板位置。默认情况下,只有两个loader,文件系统模板DIRS和app模板/&lt;project&gt;/&lt;app&gt;/templates/*,你可以混合任何位置,只要能找到。

标签: python django for-loop inheritance


【解决方案1】:

在您的应用中,模板文件夹结构必须类似于:

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

Templates 目录必须在您的应用程序文件夹中,并且其中必须是另一个具有您应用程序名称的文件夹,并且其中必须是模板文件。

编辑

如果您的模板位于应用模板文件夹中,您应该将 DIRS 更改为空列表: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',
            ],
        },
    },
] 

【讨论】:

  • 嘿,我刚刚尝试过,但仍然如此。那我应该更改 settings.py 路线吗?反正也没用
  • 嘿,我又跑了,是编辑器的问题,路线变了,又跑了,现在可以了!!在继续使用这些模板更改进行项目时,我应该考虑什么吗? (甚至设置路线或文件夹位置更改)。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
相关资源
最近更新 更多