我在Windows XP上运行的Django 1.3,Python 2.7版问题链接到静态文件在Django 1.3
我想设置一个静态文件夹中的CSS我的Django应用程序。
模板的样子:
<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 my.css:\ ROOT \工作区\ mywebapp的\ src \ mywebapp \ css_demo \静\ 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/
谢谢!
hmm ...不,我使用runserver在端口80上启动它0.0.0.0:80 – 2011-05-21 21:45:49
绝对URL并非只是不需要,但根本不起作用。如果你设置STATIC_URL ='http:// localhost:8000/static /',它会产生怪异的错误,比如破损的管道和404s的静态文件。发生这种情况的原因对我来说是未知的,但在https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-发展 – 2013-02-28 02:11:02