2013-04-01 48 views
0

编辑:请不要误导这个问题,这是我的错,我的变化,不工作。我碰巧需要命名为相同的项目(not a smart idea),这使我感到困惑,因为我正在从一个不同的项目中进行更改,对此我误导了很好的stackoverflow人员。为什么模板继承失败?

我有一个基本模板public_base.html

<!doctype html> 
<!-- [if IE ]> <html class="no-js"> <![endif] --> 
<head> 

    <!-- favicon for web browser and smartphones --> 
    <link rel="icon" href="{{ STATIC_URL }}images/img.png" type="image/x-png"> 
    <link rel="apple-touch-icon" href="{{ STATIC_URL }}img/apple-touch-icon.png"> 

    <!-- Google Fonts --> 
    <link href='http://fonts.googleapis.com/css?family=Homenaje|Molengo' rel='stylesheet' type='text/css'> 

    <!-- CSS Section --> 
    <link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}maincss/bootstrap.min.css"> 

    <link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}maincss/impress.css"> 
    <link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}maincss/style.css"> 
    <link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}maincss/impression.css"> 


    <title>{% block title %}F4L | Have it easy{% endblock %}</title> 
</head> 

<body lang="en"> 

    <div id="container"> 
    {% load smartmin %} 
    <div id="header">                  <!-- Header --> 
     <div id="logo"> 
     <a href="{% url homepage %}"> 
      <img src="{{ STATIC_URL }}img/logo.jpg" class="" alt="" /> 
     </a> 
     </div> 
     <div id="menu"> 
     <ul> 

     {% if perms.restaurant_detail.restaurant_detail.create %} 
      <li><a href="#">Join F4L</a></li> 
     {% endif %} 
     <li><a href="#">FAQ</a></li> 
     <li><a href="#">contact</a></li> 
      <li><a href="#">about us</a></li> 

     {% if request.user.is_authenticated %} 
     <li><a href="{% url users.user_logout %}">logout</a></li> 

      {% else %} 
      <li><a href="{% url users.user_login %}">login</a></li> 
      {% endif %} 
     </ul> 
     </div> 
    </div>                    <!-- End Header --> 
    <div style="clear: both;"></div> 

    <div id="main">                  <!-- Main --> 
     {% block main-contents %} 
     <p>jrneflkwnel</p> 
     {% endblock %} 
    </div> 

    </div> 

,我这里继承,home.html

{% extends "public_base.html" %} 

{% block main-contents %} 

<p>This is not Working.</p> 

{% endblock main-contents %} 

但这不工作,基本上都在home.html内容不加载。这可能是什么原因造成的?

回答

0

您需要继承块在home.html

{% extends "public_base.html" %} 
{% block main-contents %} 
     <p>This is not Working.</p> 
{% endblock %} 

有通过the docs on template inheritence阅读,具体如下:

[当您从父模板继承]模板引擎会注意到base.html中的三个块标签,并将这些块替换为子模板的内容。

所以,当你从父模板继承,你需要在你的孩子模板块覆盖块父模板中,在这种情况下,main-contents块。

+0

做到了这一点,仍然没有改变... –

+0

这两个模板都在你的'templates'文件夹的根目录吗? –

+0

nope,'home.html'不是..它在另一个目录中...... –

0

您必须在您的public_base.html中定义一个空的block content。在您的public_base.html中添加以下代码。

{% block content %} 
{% endblock %} 

要不然就得从public_base.html继承{% block main-contents %}

+0

那也没有解决问题...... –

+0

你的应用程序的settings.py中定义了根模板文件夹中的模板吗? – arulmr

+0

不,'home.html'在另一个目录中... –