【问题标题】:django static filesdjango 静态文件
【发布时间】:2011-03-06 15:26:48
【问题描述】:

我的 urls.py 行中有:

if settings.DEBUG==True:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_DOC_ROOT})
    )

在我的设置文件中是这样的:

STATIC_DOC_ROOT = os.path.join(os.path.dirname(__file__),'static').replace('\\','/')

当我在我的页面中转到 / 时,一切都很好。当我单击从/到另一个页面的链接时,由于 404 错误,我没有应用 CSS。他尝试从该 URL 加载 CSS 文件。

http://localhost:8000/show/sth/static/style.css

这是我的 urls.py 这个方法

(r'^show/(?P<subject>[^/]+)/(?P<title>[^/]+)$','show'),

我有 url /links 和 css 只在这个模板中工作它不起作用。似乎这些参数搞砸了。有什么建议为什么会失败? 这是我的模板代码:

{% extends "szkielet.html" %}
{% block tresc %}
    <div id="content">
        <div class="post">
            <h2 class="title">{{ notatka.tytul }}</h2>
                <p class="meta"><span class="author">{{ notatka.user.name }}</span> <span class="date">July 07, 2010</span>&nbsp;<span class="links"><a href="#" title="">Comments</a></span></p>
                <div class="entry">
                    <p>{{ notatka.tresc }}</p>
            </div>
        </div>
    </div>
{% endblock %}

Szkielet.html - 这是我的基地

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Perfect Blemish      by Free CSS Templates</title>
<link href="{{ MEDIA_URL }}style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>

这里是负责 CSS 的部分,它只是一个样式表。

【问题讨论】:

  • 我猜你有一个基本的 html 来扩展所有的模板。您是否忘记在此页面上扩展。
  • 我有扩展部分。问题是在那个模板中,由于路径错误,css 无法正常工作

标签: django static django-templates


【解决方案1】:

URL 可能被设置为覆盖静态 url 路径,但这里不是这种情况!因此,您的 URL conf 与此无关。

核心问题是您的模板呈现了错误的 URL:http://localhost:8000/show/sth/static/style.css 应该是 http://localhost:8000/static/style.css 正确吗?

向我们展示您的模板,因为这似乎是问题所在。在我看来,您只是为 CSS 设置了相对 URL,而不是绝对 URL 或 /static/style.css

你的{{ media_url }} 是什么?应该是绝对的,以/开头

【讨论】:

  • 使用 url,你是 100% 正确的,这正是它的行为方式和行为方式。我在第一篇文章中添加了代码
  • 您必须发布具有 CSS 文件链接的模板
  • 因此请确保您的 media_url 不是相对 url。确保它以/ 开头或包含媒体的完整路径http://localhost:8000/static/
  • 抱歉回答晚了。我忘记在开始时在 MEDIA_URL 中添加 /。当我现在添加它时,一切正常,再次感谢 Yuji
猜你喜欢
  • 2014-02-26
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 2017-12-23
  • 2012-09-30
  • 2018-07-10
  • 2014-06-03
相关资源
最近更新 更多