Programming

Serving Staric media In Django project

Hello, Here I caome with another post serving media content

I am writing about this because I felt this to be most important and will be used in serving media content like CSS, Images and other stuff. Now let us see how we can do this,

Settings.py:

There are three settings to get right:: MEDIA_ROOTMEDIA_URL and ADMIN_MEDIA_PREFIX.

MEDIA_ROOT is the absolute filesystem path where your media files are. I usually set it like::

MEDIA_ROOT = os.path.join(
    os.path.abspath(os.path.dirname(__file__)),
    'media',
)

This will set MEDIA_ROOT to point to the media directory in your project directory. MEDIA_URL and ADMIN_MEDIA_PREFIX are URL’s:

MEDIA_URL = ‘/media/’
ADMIN_MEDIA_PREFIX = ‘/media/admin/’

With this setup, to serve admin media in production, all I need to do is to symlink media folder of admin app into my media directory. Of course you can set MEDIA_URL to point to another domain/subdomain

URL Configuration:

if settings.DEBUG:
from django.views.static import serve
_media_url = settings.MEDIA_URL
if _media_url.startswith(‘/’):
_media_url = _media_url[1:]
urlpatterns += patterns(”,
(r’^%s(?P.*)$’ % _media_url,
serve,
{‘document_root’: settings.MEDIA_ROOT}))
del(_media_url, serve)

settings.DEBUG == True doesn’t necessarily mean development server is running. But it is a good indicator since deploying with development server is not a good idea for many reasons. Notice here we don’t serve media unless MEDIA_URL is an absolute URL on our server.

Templates

Finally we need to specify media URL’s correctly. To avoid hard-coding media path we will be using {{ MEDIA_URL }} context variable in our templates. To have {{ MEDIA_URL }} included automatically in each template we need to do two things:

Make sure you have django.core.context_processors.media in your TEMPLATE_CONTEXT_PROCESSORS.
Make sure each view is using a RequestContext.

Afterwards all we need to do is to specify our media URL’s like this:

<img src="{{ MEDIA_URL }}img/header.jpeg" />

This will be translated to:

<img src="/media/img/header.jpeg" />

Tips and Tricks:
why not serve our 500 and 404 pages statically
When DEBUG == True, 500 (server error) and 404 (not found) situations are handled with special debugging views. So there’s no chance to test your error pages. Add the following code, just like static serving code:

if settings.DEBUG:
urlpatterns += patterns(”,
(r’^404/’,
‘django.views.generic.simple.’ \
‘direct_to_template’,
{‘template’: ‘404.html’}),
(r’^500/’,
‘django.views.generic.simple.’ \
‘direct_to_template’,
{‘template’: ‘500.html’}))

Now when you visit /500/ and /404/ on your development server you will be served a fake error page.

Thats it,

Hope you have liked this post. Post your comments and queries.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s