Monday, June 21, 2010

Serving media files in Django

Serving media files

Django doesn't serve media files itself; it leaves that job to whichever Web server you choose.
We recommend using a separate Web server -- i.e., one that's not also running Django -- for serving media. Here are some good choices:
If, however, you have no option but to serve media files on the same Apache VirtualHost as Django, you can set up Apache to serve some URLs as static media, and others using the mod_wsgi interface to Django.
This example sets up Django at the site root, but explicitly serves robots.txt, favicon.ico, any CSS file, and anything in the /media/ URL space as a static file. All other URLs will be served using mod_wsgi:

Alias /media/ /usr/local/wsgi/static/media/

<Directory /usr/local/wsgi/static>
Order deny,allow
Allow from all

WSGIScriptAlias / /usr/local/wsgi/scripts/django.wsgi

<Directory /usr/local/wsgi/scripts>
Order allow,deny
Allow from all

More details on configuring a mod_wsgi site to serve static files can be found in the mod_wsgi documentation on hosting static files.

Problem : -

Myhttp://localhost/admin/ is display in  pattern without grapic or  image not  like that which is shown in Tutorial

 MY admin page (http://localhost/admin/)

Django administration
 Welcome, Jagdeep. Change password / Log out
 Site administration
 Groups  Add     Change
 Users   Add     Change
 Polls   Add     Change
 Sites   Add     Change
 Recent Actions
 My Actions

 where is the problem .........i am not able to find???

After a Long discussion on  django-user mailing list  i am able to find the solution 

Solution  :-
Major problem is giving the  path  wrong path of media directory.

httpd.conf  apacha file editing

Alias /media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/

<Directory /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/>
Options Indexes
Order deny,allow
Allow from all

WSGIScriptAlias / /home/jagdeep/mysite/apache/django.wsgi

<Directory /home/jagdeep/mysite/apache/>
Order allow,deny
Allow from all

and after this set the default setting of  file of django .


  1. Good post man, I was struggling with my modwsgi setup and serving static files, but I finally nailed it down. On my end the Alias statements needed to have trailing slashes, whereas statements didn't.