Django 扩展模板

发布于 2024-08-04 17:00:08 字数 2472 浏览 8 评论 0原文

我有简单的 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>  

i have simple django/python app and i got 1 page - create.html. So i want to extend this page to use index.html. Everything work (no errors) and when the page is loaded all data from create.html and all text from index.html present but no formating is available - images and css that must be loaded from index.html is not loaded. When i load index.html in browser looks ok. Can someone help me?

Thanks!

here is the code of templates:

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 %}

and 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>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜扑 2024-08-11 17:00:08

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

It looks like you are extending base.html and not index.html.

百善笑为先 2024-08-11 17:00:08

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

  {% extends "base.html" %}

将其更改为

  {% extends "index.html" %}

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

More specifically, look at the first line of your content.html:

  {% extends "base.html" %}

Change this to

  {% extends "index.html" %}

(or rename index.html to be base.html)

叫思念不要吵 2024-08-11 17:00:08

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

Django 模板可以看不到 CSS 文件

Ahaa find where is the problem!
MEDIA_ROOT and MEDIA_URL was not set up :-( after edit them everything work ok.

Django template can't see CSS files

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文