在django虚拟服务器上使用静态文件

时间:2012-06-14 12:59:16

标签: css django static-files django-staticfiles

之前已经问过这样的问题,我已经阅读了所有这些并尝试理解关于该主题的官方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

(目前没有应用程序文件夹,因为我正在跟随“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仍然没有被应用。enter image description here你能帮我弄清楚我做错了什么吗?我会非常感激。

我正在使用最新版本的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,
            }),
    )

2 个答案:

答案 0 :(得分:3)

如果您使用的是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,
            }),
    )

同样在您的设置中,通常的做法是避免使用硬编码位置,这是一个示例(顺便说一下,您的设置中是否有静态文件搜索器?

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",
)

答案 1 :(得分:0)

您应该像这样写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' 

为了确保,请使用Firebug或webkit调试器检查是否已加载CSS。

请记住,如果settings.DEBUG == False,Django将不会为您的静态文件提供服务。