【问题标题】:Django template {% include not workingDjango 模板 {% include 不工作
【发布时间】:2015-08-18 08:02:27
【问题描述】:

我是 Django 新手。我在主模板中包含子模板时遇到问题。我的项目的目录结构附在快照中。我删除了默认的 views.py 并创建了自己的名为“views”的文件夹并将我的视图放入其中。这是我所做的:

1. app/views/__init__.py

from .home import *

2。 app/views/home.py

from django.shortcuts import render

def index(request):
    return render(request, 'home.html')

3. app/templates/home.html

{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
    Administrator Section
</title>

<link type="text/css" rel="stylesheet" href="{% static "css/common.css" %}"/>
<link type="text/css" rel="stylesheet" href="{% static "css/theme/transdmin.css" %}"/>
<link type="text/css" rel="stylesheet" href="{% static "css/login.css" %}"/>

<script type="text/javascript" src="{% static "js/jquery-1.10.2.js" %}"></script>
<script type="text/javascript" src="{% static "js/common.js" %}"></script>
<script type="text/javascript" src="{% static "js/login.js" %}"></script>

</head>
<body>
<div>

<div id="wrapper" class="ys-adminform">

    {% include "includes.header_logo.html" %}

    <form id="form-login" class="admin-section-form frm">

        <div class="header">
            <br/>

            <h1>Login</h1>
            <br/>
        </div>

        <div class="content">

            <div class="form-row">
                <input name="email" class="input" placeholder="Email" type="text"/>

                <div class="user-icon"></div>
            </div>
            <div class="form-row">
                <input name="password" class="input password" placeholder="Password" type="password"/>

                <div class="pass-icon"></div>
            </div>
        </div>

        <div class="footerlogin">
            <input class="button" name="btn-login" value="Authenticate" type="button"/>

            <div class="message" style="font-weight: bold; padding-top:16px;">&nbsp;</div>
        </div>

    </form>

</div>
</div>

{% include "includes.footer.html" %}
</body>
</html>

问题是 include 没有从子视图中添加内容。 我知道这可能是路径问题,但我尝试了各种选项,例如:

{% include "includes.header_logo.html" %}

{% include includes.header_logo.html %}

{% include "includes/header_logo.html" %}

{% include "templates.includes.header_logo.html" %}

{% include "app.templates.includes.header_logo.html" %}

等等

【问题讨论】:

  • 你能传递一个包含的模板吗?为什么你不使用{% extends %} 模板标签呢?最好(在 django 中更喜欢)拥有一个基本文件并用您的代码扩展它

标签: python django django-templates


【解决方案1】:

您必须使用目录斜杠 (/),而不是点 (.)

例如:

{% include "includes.footer.html" %}

变成这样:

{% include "includes/footer.html" %} 

【讨论】:

  • 我试过斜线,但还是一样。即使它没有显示任何错误。我希望您在查看附加快照中的目录结构后提出建议
  • 感谢 Leistungsabfall,您的解决方案有效。图片导致了我将检查的问题。
【解决方案2】:

正如 Leistungsabfall 所建议的,“includes/footer.html”有效。标头不起作用,因为它有:

这一行一定是崩溃了,因为哪个标题不起作用。

【讨论】:

    【解决方案3】:

    只需添加带有斜线的模板文件夹 .... 像 {% 包括 "blog/footer.html" %}

    【讨论】:

      猜你喜欢
      • 2012-09-10
      • 2019-06-26
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多