Django模板块不显示?

1 投票
1 回答
7584 浏览
提问于 2025-04-17 00:16

我在处理一个问题,涉及到在子模板的块标签中显示内容。我有一个 base.html 文件和一个 dash.html 文件,dash.html 是从 base.html 继承的。当我打开 dash.html 页面时,base.html 的内容是加载了,但 dash.html 中块标签里的内容却没有显示出来。所以我在打开 base.html 和 dash.html 时看到的内容是一样的。

这是我的代码:

base.html

<!DOCTYPE HTML>
<!--[if lt IE 7 ]> <html class="no-js ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]>    <html class="no-js ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]>    <html class="no-js ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>SCV Discount</title>
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="-1">
    <link rel="stylesheet" href="static/css/main.css">
    <script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.0.6/modernizr.min.js"></script>
</head>
<body>
<!-- Main Header -->
<div id="top">
    <div id="header">
        <a href="/"><div id="logo"></div></a>
        {% block head %}{% endblock %}
    </div>
</div>

<!-- Middle -->
<div id="mid">
    {% block mid %}{% endblock %}
</div>

<!-- Content -->
<div id="analytics-content">
    {% block main %}{% endblock %}
</div>

<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="static/js/libs/jquery-1.6.2.min.js">\x3C/script>')</script>
<script src="static/js/libs/select.js"></script>
<script src="static/js/main.js"></script>
</body>
</html>

dash.html

{% extends 'base.html' %}

{% block head %}
    <p class="dashboard-title">Elephant Bar</p>
    <p class="cp_badge round">Control Panel</p>
{% endblock %}

{% block mid %}
    {% include 'modules/meters.html' %}
    {% include 'modules/client_ctl.html' %}
{% endblock %}

{% block main %}
    {% include 'modules/history.html' %}
{% endblock %}

view.py

from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.template import RequestContext
import datetime

def index(request):
    return render_to_response('base.html', RequestContext(request))

def dash(request):
    return render_to_response('dash.html', RequestContext(request))

希望这些信息足够了。如果需要我提供其他信息,请告诉我。提前谢谢你们,我很感激。

1 个回答

2

我刚把这段代码复制粘贴到一个新项目里,它运行得很好。你的urls.py文件是什么样的?你有没有可能把你的dash网址发到了首页视图?这是我写的一个简单的urls.py,让这个功能可以正常运行:

from django.conf.urls.defaults import *
import views

urlpatterns = patterns('',
    (r'^index/', views.index),
    (r'^dash/', views.dash),
)

当你在浏览器中查看dash.html(通过你想要与dash视图关联的任何网址)时,如果你看不到任何特定于dash.html的代码,你怎么知道它在扩展base.html呢?

撰写回答