【问题标题】:Problems linking to static files in Django 1.3在 Django 1.3 中链接到静态文件的问题
【发布时间】:2011-08-29 18:09:39
【问题描述】:

我在 Windows XP 上运行 django 1.3、python 2.7

我正在尝试在我的 django 应用程序的静态文件夹中设置一个 css。

模板如下:

<html>
    <head>
        <title>css demo</title>
        <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/my.css" />

    </head>
    <body>

生成的 HTML 如下所示:

<html> 
    <head> 
        <title>css demo</title> 
        <link rel="stylesheet" type="text/css" href="http://localhost/static/css/my.css" /> 

    </head>
...

看来我已经设置好了所有东西,以便指定静态文件在模板中的位置,然后在生成的 html 中。

但是在“http://localhost/static/css/my.css”有注释。我如何获得它?

我像这样运行 collectstatic:

C:\root\workspace\mywebapp\src\mywebapp>c:\Python27\python.exe manage.py collectstatic

You have requested to collect static files at the destination location as specified in your settings file.

This will overwrite existing files.
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Copying 'c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css'

1 static file copied to 'C:/root/workspace/mywebapp/src/mywebapp/static/'.

所以我现在在 c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css 中有 my.css

在我的设置中,我有:

STATIC_ROOT = 'C:/root/workspace/mywebapp/src/mywebapp/static/'
STATIC_URL = 'http://localhost/static/'

在我的 url.py 中有:

from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = patterns('',

    (r'^mywebapp/', include('mywebapp.css_demo.urls')),
)

urlpatterns += staticfiles_urlpatterns()

所以如果我理解正确,我的 css 在:

c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css

不应该在:

http://localhost/static/css/my.css

但我看到的是:

Page not found (404)
Request Method: GET
Request URL:    http://localhost/static/css/my.css
Using the URLconf defined in mywebapp.urls, Django tried these URL patterns, in this order:
^mywebapp/
The current URL, static/css/my.css, didn't match any of these.

我也试过了:

http://localhost/mywebapp/static/css/my.css
http://localhost/mywebapp/css_demo/static/css/my.css

我在这里错过了一步吗? 这方面的文档有点混乱。 http://docs.djangoproject.com/en/1.3/howto/static-files/ http://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/

谢谢!

【问题讨论】:

  • 如果虚假广告法适用于 FOSS,则“此文档有点混乱”将替换每个 djangoproject.com 页面顶部的“为完美主义者提供截止日期的 web 框架”。

标签: python django static-files


【解决方案1】:

我创建了一个文件夹:

C:\root\workspace\mysite\src\mysite\common

在我的设置中,我有:

STATIC_ROOT = 'C:/root/workspace/mysite/src/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    "C:/root/workspace/mysite/src/mysite/common/static",
)

这似乎有效。

【讨论】:

    【解决方案2】:

    我敢打赌,您的应用程序在 http://localhost:8000 运行,而不是 http://localhost :)

    改变

    STATIC_URL = 'http://localhost/static/'
    

    STATIC_URL = '/static/'
    

    那里不需要绝对 URL。

    【讨论】:

    • 嗯...不,我在端口 80 上使用 runserver 0.0.0.0:80 启动它
    • 绝对 URL 不仅不需要,而且根本不起作用。如果您设置 STATIC_URL = 'localhost:8000/static' 它会产生奇怪的错误,例如静态文件的管道损坏和 404。发生这种情况的原因对我来说是未知的,但在docs.djangoproject.com/en/dev/howto/static-files/…的文档中提到了一些关于这种行为的内容@
    猜你喜欢
    • 2022-06-13
    • 1970-01-01
    • 2013-04-06
    • 2019-05-28
    • 2018-10-21
    • 1970-01-01
    • 2013-10-22
    • 2011-06-11
    • 2020-11-16
    相关资源
    最近更新 更多