views:

149

answers:

3

My Django templates use a lot of related stuff: images, style sheets, etc.

Where should I put these file, or how should I refer to them in the template itself?

For now I'm using the development server.

I know it's a really common thing, but I can't really figure it out.

+5  A: 

I put them inside a folder named static, which is in the web project's top level folder.

Example:

/static/img/
/static/js/
/static/css/
/templates/
urls.py
settings.py

I then have the following rule in my urls.py file:

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

My settings.py contains:

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static').replace('\\', '/')
ADMIN_MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static/admin').replace('\\', '/')
Brian R. Bondy
We call that folder "media".
S.Lott
Aaah! This was a bit tricky. I've tried to name my folder `media`, but of course it conflicted with the admin's media folder.
shinjin
shinjin: So provide ADMIN_MEDIA_PREFIX in the settings as something else, say /admin-media/
Lakshman Prasad
+2  A: 

Maybe you can read the doc http://docs.djangoproject.com/en/dev/howto/static-files/#howto-static-files

diegueus9
A: 

We put ours under /media. Everything that is specifically tied to the layout of the sites is further separated. Of course none of this static content is served by Django on the production site. They often aren't even on the same physical server.

/media
    /images - this is for content-specific images
    /video - these next 2 are normally symlinks to a /big_content folder ...
    /audio - so that they aren't included in our mercurial repository.
    /layout - everything that is tied to the basic templates.
        /css
        /js
        /images
Peter Rowell