Django扩展模板不显示内容

时间:2015-11-04 22:39:55

标签: django django-templates

关于这个话题还有其他几个主题,但是特别是一个回答urls.py问题并没有解决我的问题......

我试图扩展基本模板。我有两个文件,base.html和index.html。 Base有基本的导航元素,CSS等,而index.html扩展了base。我在我的视图中指向index.html,但我只能看到base.html中的代码 - index.html代码未显示。任何帮助表示赞赏。此外,任何代码都没有报告任何错误。

views.py:

class DashboardView(TemplateView):

    context_object_name = 'home_list'
    template_name = 'index.html'
    queryset = Media.objects.all()

    def get_context_data(self, **kwargs):
        context = super(DashboardView, self).get_context_data(**kwargs)
        context['jobs'] = Job.objects.all()
        return context

urls.py:

的相关部分
url(r'^admin/', include(admin.site.urls)),
url(r'^apiroot/', router.get_api_root_view()),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^$', dashboard_views.DashboardView.as_view(), name="home_list"),
模板目录中的

base.html :(注意"测试。"部分显示在屏幕上,但这是最后一个元素加载页面。)

{# load the tag library #}
{% load bootstrap3 %}

{% load staticfiles %}

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="{{STATIC_URL}}bootstrap3/css/bootstrap.min.css" rel="stylesheet">

{# load CSS and Javascript #}
{% bootstrap_css %}
{% bootstrap_javascript %}

{# Display django.contrib.messages as bootstrap alerts #}
{% bootstrap_messages %}

<div class = "container">
Backup Administraton  (test)
</div>

<!------Navbar ---------->

<header class = "navbar navbar-inverse navbar-fixed-top" role = "banner">
<div class = "container">
    <div class ="navbar-header">
        <button type = "button" class= "navbar-toggle" data-toggle = "collapse" data-target = "#dropdown">
            <span class = "sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>

        <a href="/" class ="navbar-brand">Backup Administration</a>
    </div><!-- End Navbar Header-->

    <div class = "collapse navbar-collapse" id = "dropdown">
        <ul class = "nav navbar-nav navbar-right" role = "navigation">
            <li><a href="/">Home</a></li>
            <li><a href="#">Reports</a></li>
            <li><a href="#">Snapshots</a></li>
            <li><a href="#">Restores</a></li>
        </ul>

    </div><!-- End Nav Contents -->
</div><!-- End Container -->
</header>



Test.

index.html 的内容:

{% extends "base.html" %}
<BR><BR><BR><BR>

Testing contents of index.html

{% block content %}
<BR><BR><BR><BR><BR>
This is index.html<BR>

{% endblock %}

index.html内容(即&#34;测试内容&#34;)从不显示在屏幕上。 FWIW,BR,是为了确保文字没有显示在黑色导航栏后面。

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

您需要向base.html添加内容块,例如:

...
<div class = "container">
Backup Administraton  (test)
</div>
<div class="container">
{% block content %}
{% endblock content %}
</div>
<!------Navbar ---------->
...