views:

46

answers:

2

in my app, i want to use a css 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

+2  A: 

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.

Dmitry Gladkov
i've used this method, thanks a lot! it was not necessary to install the annoying middleware, for now it wors fine without:DThanks again!
dana
surely it isn't necessary, but I really like django-annoying app and thought you may find it useful:)
Dmitry Gladkov
i'll review it in case of trouble ;) thanks!:)
dana
+2  A: 

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.

Daniel Roseman
though i've read it 3 times, i really don't understand what you mean.should i change the STATIC_DOC_ROOT path to the absolute one on my disk or?...
dana
Yes, that's exactly what I mean.
Daniel Roseman
i'll try it now, and post the result :) thanks a lot!:)
dana
it works now, but i've used an url, importing and using the 'os' in the settings.py. it is too much headache with the absolute path, and it seems hardcoded:DThanks so much for support!:)
dana