0

可能重复:
Django 和服务静态文件

我在 base.html 加载 CSS 时遇到了一个问题。我把所有的css文件放在/static目录下。

urls.py我把这段代码:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve',
           { 'document_root': '/home/bkcherry/botstore/botstore/static' }),
    )

在 base.html 我放了以下内容:

<link rel="Stylesheet" type="text/css" href="/static/css.css" />

当我转到 main.html 时,css 样式不起作用。我需要配置 settings.py MEDIA_ROOTMEDIA_URL还是STATIC_ROOT

4

3 回答 3

1

不能使用 MEDIA_ROOT 或 MEDIA_URL 这是上传的媒体而不是你的静态内容,你不需要设置 URL 模式,因为这仅适用于 django 1.2 或“如果你使用其他服务器进行本地开发”:https:/ /docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development

你需要有你的静态文件:botstore/botstore/static/botstore/css.css

然后使用:

HOME_ROOT = os.path.dirname(__file__)

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"

STATIC_ROOT = os.path.join(HOME_ROOT, 'staticfiles')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'

# 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',
)

然后在您的 HTML 中,您可以这样引用您的静态文件:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}botstore/css.css" />
于 2012-10-16T18:27:58.513 回答
0

如果您查看官方文档

from django.conf import settings

# ... the rest of your URLconf goes here ...

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
    )

MEDIA_ROOT 最后应该有 / (https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-MEDIA_ROOT

于 2012-10-16T16:11:53.070 回答
0

我认为您需要在路径末尾使用斜杠,即'/home/bkcherry/botstore/botstore/static/'

于 2012-10-16T16:06:31.623 回答