【问题标题】:Django and extending templates not working?Django 和扩展模板不起作用?
【发布时间】:2021-01-02 20:10:11
【问题描述】:

我正在使用 Django 构建我的博客站点,但我无法在我的项目中扩展模板。

我有两个模板:

  1. base.html
  2. post.html

Base.html 是我的父模板,post.html 是我扩展 base.html 的子模板。

我无法让 Django 扩展 post.html。当我运行 web 服务器 base.html 显示但没有来自 post.html 的文本。

我已经阅读了关于模板以及如何扩展的 Django 文档,我相信我必须正确使用语法,但我不确定它为什么不起作用?

我也在这个项目中使用 Boostrap。

感谢您的帮助, 耐尔敏

post.html

  {% extends "post/base.html" %}

{% block content %}
    <div class="blog-post">
            <h2 class="blog-post-title">Sample blog post</h2>
            <p class="blog-post-meta">January 1, 2014 by <a href="#">Mark</a></p>

            <p>Blog text goes here.</p>


    </div><!-- /.blog-post -->
{% endblock  %}

在此处输入代码

base.html

  <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <!-- load static files -->
    {% load staticfiles %}

    <title>Blog Title</title>

    <!-- Bootstrap core CSS -->
    <link href="{% static 'css/bootstrap.min.css'  %}" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="../../assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="{% static 'css/blog.css'  %}" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="../../assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>

    <div class="blog-masthead">
      <div class="container">
        <nav class="blog-nav">
          <a class="blog-nav-item active" href="#">Home</a>
          <a class="blog-nav-item" href="#">About</a>
        </nav>
      </div>
    </div>

    <!-- blog post goes here -->

    {% block content %}
    {% endblock %}


    <div class="container">

      <div class="blog-header">
        <h1 class="blog-title">Code'N Cofee Blog</h1>
        <p class="lead blog-description">Live and write code. </p>
      </div>

      <div class="row">

        <div class="col-sm-8 blog-main">


          <nav>
            <ul class="pager">
              <li><a href="#">Previous</a></li>
              <li><a href="#">Next</a></li>
            </ul>
          </nav>

        </div><!-- /.blog-main -->

        <div class="col-sm-3 col-sm-offset-1 blog-sidebar">
          <div class="sidebar-module sidebar-module-inset">
            <h4>About</h4>
            <p>Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p>
          </div>
          <div class="sidebar-module">
            <h4>Archives</h4>
            <ol class="list-unstyled">
              <li><a href="#">March 2014</a></li>
              <li><a href="#">February 2014</a></li>
              <li><a href="#">January 2014</a></li>
              <li><a href="#">December 2013</a></li>
              <li><a href="#">November 2013</a></li>
              <li><a href="#">October 2013</a></li>
              <li><a href="#">September 2013</a></li>
              <li><a href="#">August 2013</a></li>
              <li><a href="#">July 2013</a></li>
              <li><a href="#">June 2013</a></li>
              <li><a href="#">May 2013</a></li>
              <li><a href="#">April 2013</a></li>
            </ol>
          </div>
          <div class="sidebar-module">
            <h4>Elsewhere</h4>
            <ol class="list-unstyled">
              <li><a href="#">GitHub</a></li>
              <li><a href="#">Twitter</a></li>
              <li><a href="#">Facebook</a></li>
            </ol>
          </div>
        </div><!-- /.blog-sidebar -->

      </div><!-- /.row -->

    </div><!-- /.container -->

    <footer class="blog-footer">
      <p>Blog template built for <a href="http://getbootstrap.com">Bootstrap</a> by <a href="https://twitter.com/mdo">@mdo</a>.</p>
      <p>
        <a href="#">Back to top</a>
      </p>
    </footer>


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
    <script src="../../dist/js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
  </body>
</html>

【问题讨论】:

    标签: python html django templates


    【解决方案1】:

    Nermic Kekic 的回答对我不起作用。

    我不是专家,但以下内容对我有用。

    问题是我的 base.html(parent) 不知道在哪里附加 post.html(child) 所以我不得不提及

    {% block content %}
    {% endblock %}
    

    base.html 正文中的这两行也对我有用,因为这个块会告诉 post.hml 在哪里显示孩子的内容。

    【讨论】:

      【解决方案2】:

      我找到了解决方案。问题不在于这两个 html 文件,而在于我的 views.py 文件。基本上在views.py中我的索引函数是渲染base.html,因此post.html屏幕上没有显示任何内容

      鉴于 post.html 正在扩展 base.html,我需要编辑我的索引函数以返回和呈现 post.html,如下所示,这修复了代码。

      希望这对遇到类似问题的其他人有所帮助。

      这就是我的 index 函数现在在 views.py 中的样子

          from django.shortcuts import render
      from django.http import HttpResponse
      
      
      def index(request):
          return render(request, 'post/post.html', {'': ''})
      

      【讨论】:

        【解决方案3】:
        {% block content %}
        {% endblock content %}
        

        您需要在base.html 文件中添加以上代码行。在您的 (post.html) 文件中,您需要添加这些行以及其中的独特内容。这样 Django 就知道你可以在哪里修改你的 post.html 文件。

        【讨论】:

          猜你喜欢
          • 2012-01-31
          • 2011-04-10
          • 2022-11-18
          • 1970-01-01
          • 2017-04-24
          • 2012-01-10
          • 2018-06-03
          • 1970-01-01
          • 2010-11-27
          相关资源
          最近更新 更多