【问题标题】:Django extends templateDjango 扩展模板
【发布时间】:2010-11-27 14:27:15
【问题描述】:

我有简单的 django/python 应用程序,我有 1 页 - create.html。所以我想扩展这个页面来使用 index.html。一切正常(没有错误),当页面加载时,来自 create.html 的所有数据和来自 index.html 的所有文本都存在,但没有可用的格式 - 未加载必须从 index.html 加载的图像和 css。当我在浏览器中加载 index.html 时看起来不错。有人可以帮我吗?

谢谢!

这是模板的代码:

create.html

    {% extends "index.html" %}

{% block title %}Projects{% endblock %}

{% block content %}
    {% if projects %}
        <table  border="1">
            <tr>
                <td align="center">Name</td>
                <td align="center">Description</td>
                <td align="center">Priority</td>
                <td align="center">X</td>
            </tr>
            {% for p in projects %}
            <tr>
                <td> <a href="/tasks/{{p.id}}/">{{p.Name}}</a> </td>
                <td>{{p.Description}} </td>
                <td> {{p.Priority.Name}} </td>
                <td> <a href="/editproject/{{p.id}}/">Edit</a> <a href="/deleteproject/{{p.id}}/">Delete</a> </td>
            <tr>
            {% endfor %}
        </table>
    {% else %}
        <p>No active projects.</p>
    {% endif %}
{% endblock %}

和 index.html:

    <html>
    <head>
        {% block title %}{% endblock %}

        <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
    </head>
    <body>
    {% block content %}{% endblock %}

<div class="PostContent">

<img class="article" src="images/spectacles.gif" alt="an image" style="float: left" />
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
<p>Lorem ipsum dolor sit amet,
<a href="#" title="link">link</a>, <a class="visited" href="#" title="visited link">visited link</a>, 
 <a class="hover" href="#" title="hovered link">hovered link</a> consectetuer 
adipiscing elit. Quisque sed felis. Aliquam sit amet felis. Mauris semper, 
velit semper laoreet dictum, quam diam dictum urna, nec placerat elit nisl 
in quam. Etiam augue pede, molestie eget, rhoncus at, convallis ut, eros.</p>

    ....
    </body>
    </html>  

【问题讨论】:

  • 能否包含这些模板的代码?

标签: python django extends


【解决方案1】:

看起来您是在扩展 base.html 而不是 index.html。

【讨论】:

    【解决方案2】:

    更具体地说,查看 content.html 的第一行:

      {% extends "base.html" %}
    

    改成

      {% extends "index.html" %}
    

    (或将 index.html 重命名为 base.html)

    【讨论】:

    • 对不起,我尝试了一些东西并留下了“base”,但无论何时 index.html 问题仍然存在
    【解决方案3】:

    啊哈找出问题所在! MEDIA_ROOT 和 MEDIA_URL 未设置 :-( 编辑后一切正常。

    Django template can't see CSS files

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-24
      • 2021-10-08
      • 2017-04-10
      • 2012-01-10
      • 2016-02-23
      • 2010-11-27
      • 2011-10-14
      相关资源
      最近更新 更多