2013-04-01 46 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'在另一個目錄中... –