Django 1.3 中链接静态文件的问题

2024-03-05

我在 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 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/howto/static-files/ http://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/ http://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/

Thanks!


我打赌你的应用程序运行在http://localhost:8000, not http://localhost :)

Change

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

to

STATIC_URL = '/static/'

那里不需要绝对 URL。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django 1.3 中链接静态文件的问题 的相关文章

随机推荐