开发者

django css file not recognised - wrong configuration?

in my app, i want to use a c开发者_JAVA技巧ss file, but the tempalte doesn't 'know' where the file is, though i've configured it as in tutorials:

in the urls.py (the urls file in the root of the site, not belonging to an app)

(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.STATIC_DOC_ROOT}),

in the template

<link href="/site_media/default.css" rel="stylesheet" type="text/css" />

in the settings:

STATIC_DOC_ROOT = '/site_media'

where can i be wrong? Thanks


Check django-annoying, it's a very useful app with plenty of convenient decorators, middlewares and functions. If you add StaticServe middleware like that, Django will serve static if DEBUG = True without explicitly setting in urls.py.

MIDDLEWARE_CLASSES = (
    'annoying.middlewares.StaticServe',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',              
    'django.middleware.doc.XViewMiddleware',               
    'django.contrib.auth.middleware.AuthenticationMiddleware',                    
)

Second, check your MEDIA_URL (in your case it's STATIC_DOC_ROOT, but you should use MEDIA_URL) and MEDIA_ROOT path. MEDIA_ROOT should point to absolute path to your static directory. This is how I do it in setttings.py:

import os


def rel(*x):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)

# MEDIA_ROOT will point to media directory where settings.py is located
MEDIA_ROOT = rel('media')
MEDIA_URL = '/media/'

You can use the same function to set path to your templates dir.


The document_root argument to static.serve needs to be the location of the file on the server filesystem, not the URL. So unless your css file really is in the /site_media directory on your disk, which seems unlikely, you want something else as STATIC_DOC_ROOT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜