« ReadyUP!VirtualBox 2.2.0 ebuilds »

Serve static files during Django development

10/04/09

Permalink 01:33:22 am, by X-Drum Email , 175 words   English (GB)
Categories: DailyRants, English, Django

Serve static files during Django development

Maybe it's old/or and documented[1] but yesterday i was looking for an easy
way to serve static files (for istance css,images,etc.) with Django using the development webserver.

After a quick search i found a solution
(ok maybe it's not the _best_ way to do it but works4me!).
Say that you want to serve images and css in with two different path,
add the following:

settings.py:

#[..]
STATIC_IMG_ROOT = '/fullpath/to/mycontent/img/'
STATIC_CSS_ROOT = '/fullpath/to/mycontent/css/'
#[..]

urls.py:

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

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

Now in your templates you can link to images and css via:

[..] awesome_header.png" width="137" height="121" [..]

and

[..] rel="stylesheet" type="text/css" href="css/main.css" [..]

Remember to disable these settings in settings.py and urls.py when you are going in production.

[1] http://docs.djangoproject.com/en/1.0/howto/static-files/

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

Feedback awaiting moderation

This post has 16 feedbacks awaiting moderation...

Comments are closed for this post.

Search

May 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

XML Feeds

powered by b2evolution