3

以前有人问过类似的问题,我已经阅读了所有这些问题并试图理解有关该主题的官方 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

(目前没有应用程序文件夹,因为我正在关注“The django book”来学习并且还没有做到这一点!)

完整的 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,
            }),
    )
4

2 回答 2

3

如果您使用的是 Django 1.4,那么我将使用静态标签:

{% load static %}

<link rel="stylesheet" href="{% static "CSS/base.css" %}" type="text/css" />

你可能在你的 url 中也需要这个,在开发文件中是通过 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",
)
于 2012-06-14T13:07:06.227 回答
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.

于 2012-06-14T13:12:00.120 回答