为什么模板继承失败?

时间:2013-04-01 10:49:54

标签: django django-templates

编辑:请不要被这个问题误导,我的错误是我的变化无法解决。我碰巧遇到名称相同的项目(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中的内容未加载。可能导致这种情况的原因是什么?

2 个答案:

答案 0 :(得分: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块。

答案 1 :(得分:0)

您必须在block content中定义一个空的public_base.html。在public_base.html

中添加以下代码
{% block content %}
{% endblock %}

否则您必须从{% block main-contents %}继承public_base.html

相关问题