无法在Django上扩展html模板

时间:2017-02-25 12:08:45

标签: python django django-forms django-templates django-admin

我无法将一个html文档(child-base_home.html)扩展到另一个父html文档 - base.html 我有下一个Django项目结构:

Testdjango/
blog/
   migrations/
   templates/
       blog/
          base.html
          base_home.html
   __init.py__
   admin.py
   apps.py
   models.py
   tests.py
   urls.py
   views.py
db.sqlite3
manage.py

我的父模板是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="">
    <title>{% block title %}{% endblock %} &ndash; Blog</title>

    <!-- Bootstrap core CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/journal/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template
    <link href="starter-template.css" rel="stylesheet"> -->

    </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Blog</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">
        {% block content %}
        {% endblock %}

    </div>
    <!-- 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 src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
     </body>
</html>

我的“孩子”base_home.html有下一个代码:

{% extends "blog/base.html" %}
{% block title %}
Home
{% endblock %}
{% block content %}
Lorem bla bla
{% endblock %}

在页面中我无法从base_home.html

中看到任何内容

6 个答案:

答案 0 :(得分:1)

您的模板似乎没问题,请确保您的模板设置看起来像这样,同时您已将应用添加到 INSTALLED_APPS

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

由于base.htmlbase_home.html位于同一目录中,因此将基数扩展为

{% extends "base.html" %}

答案 1 :(得分:0)

尝试

.full-screen-video {
    width: 100vw;
    height: calc(100vw / 1.7);  /* 1.7 is 1920/1080 */
 }

而不是

{% extends "base.html" %}

答案 2 :(得分:0)

您可以通过两种方式扩展模板

  1. include ##在模板中包含html的特定部分
  2. extends ##将常见的html扩展为差异页面
  3. base.html文件

    <html>
    <head></head>
    <body>
    ======
    ======
    </body>
    </html>
    
    home.html
    
    If base.html in any folder please mention folder name dont put slash(/) before the foldername `{% extends 'foldername/base.html' %}` 
    
    {% extends 'base.html' %} ## you can see the base.html styles in the home.html
    

    假设您需要包含一个特定的div,表或代码段使用 {% include 'base.html' %}如果位于文件夹{% include 'foldername/base.html' %}

答案 3 :(得分:0)

你有类似的东西

吗?
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'DIRS': [
            str(ROOT_DIR.path('templates')),
        ],`...

在您的设置中?

您是否在项目设置INSTALLED_APPS中注册了博客应用程序?

答案 4 :(得分:0)

我想我找到了asnwer。在我看来,我有返回渲染(请求,&#34; blog / base.html&#34;)。我挑选它来返回渲染(请求,&#34;博客/ base_home.html&#34;) - 我已经扩展了。现在它的工作。请告诉我,这是真的吗?

答案 5 :(得分:-1)

Django并不知道你想要结束哪个街区。你需要定义它。

{% extends "blog/base.html" %}
{% block title %}
Home
{% endblock title %}
{% block content %}
Lorem bla bla
{% endblock content %}