问题正是这样已经问过,我读过他们都力图使关于这一问题的官方Django文档的感觉,但我仍然在努力用虚拟服务器上的静态文件。 更具体地讲我只是试图让我的模板, Base.html
,使用base.css.
我的文件夹结构如下所示:
manage.py
static/CSS/base.css
VergeGreenITEvent_Website/settings.py
VergeGreenITEvent_Website/views.py ect
VergeGreenITEvent_Website/Webpage_Templates/Base.html
(目前没有app文件夹,因为我下面的“Django的书”的学习,并没有得到该位!)
完整的settings.py在这里可以查看: http://pastebin.com/JB3mKRcJ
在我base.html文件模板,我现在的代码:
<head>
<title>Verge Green IT</title>
{% load static %}
<link rel="stylesheet" href="{% static "CSS/base.css" %}" type="text/css" />
</head>
CSS还没有被应用。 你能不能帮我找出我做错了什么? 我是令人难以置信的感谢。
我使用的是最新版本的Django的。 (1.4)
urls.py:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import views
urlpatterns = patterns('',
url(r'^$', views.Home),
)
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns() #this serves static files and media files.
#in case media is not served correctly
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
如果你使用Django 1.4,那么我会使用静态标签:
{% load static %}
<link rel="stylesheet" href="{% static "CSS/base.css" %}" type="text/css" />
你可能需要这个您网址也是如此,在发展的文件通过供应的Django
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns() #this serves static files and media files.
#in case media is not served correctly
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
此外,在您的设置是很常见的做法,以避免硬编码的地点,这里有一个例子(顺便说一下,你有你的设置静态filefinders?
PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root')
# this folder is used to collect static files in production. not used in development
STATIC_URL = "/static/"
STATICFILES_DIRS = (
('', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
# other processors...
"django.core.context_processors.static",
)
你应该写你的settings.py
这样的:
import os
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
STATIC_URL = '/static/' # Should be root relative or absolute
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
# Where your static files will be collected with colletstatic command
# I use it only in deployed configuration with DEBUG=False
# Need to be outside STATICFILES_DIR
STATIC_ROOT = '/var/www/your.site'
只是可以肯定,与CSS加载萤火虫或WebKit的调试检查。
请记住,Django将不若满足您的静态文件settings.DEBUG == False
。