Django静态文件CSS不工作

2024-09-27 00:20:32 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
Django and Serving Static Files

我有一个问题要加载CSS基本.html. 我把所有css文件放在/static目录下。在

urls.py我写了以下代码:

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

在基本.html我提出以下几点:

^{pr2}$

当我去主.html,css样式不起作用。我需要配置设置.pyMEDIA_ROOTMEDIA_URL还是{}?在


Tags: and文件django目录htmlstaticfilesurls
3条回答

我想你需要在路径的末尾加一个斜线,即'/home/bkcherry/botstore/botstore/static/'

如果你检查官方文件

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

媒体根目录末尾应该有/(https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-MEDIA_ROOT

您必须使用媒体根目录或媒体URL这是用于上载的媒体而不是静态内容,并且您不需要设置URL模式,因为这仅适用于django 1.2或“如果您正在使用其他服务器进行本地开发”:https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development

您需要将静态文件保存在: 机器人商店/机器人商店/静态/机器人商店/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中可以引用静态文件,从而:

^{pr2}$

相关问题 更多 >

    热门问题