On Java, Python, Groovy, Grails, Spring, Node.js, Linux, Arduino, ARM, Embedded Devices & Web

Configure static resources in Django – Python

Posted by Venkatt Guhesan on March 1, 2012

Working on a Django project, one of the first things that one would encounter is configuring Django for static resources such as “css”, “images”, “jpeg” and “js”. In looking at the “StaticResources” link that’s part of the Django documentation, it’s a bit cryptic for someone who’s starting out in Django and Python. So here’s a step-by-step on two ways to configure your static-resources in a Django project.

This method is described in the Django documentation and is probably the preferred approach.

1. Edit “” under your Django project and define the following variables:

STATIC_URL = '/s/'

2. Edit “” and add the following:

from spog import settings
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^s/(?P<path>.*)$', 'django.views.static.serve',
           {'document_root': settings.STATIC_ROOT}),

Notice the extra comma at the end. That’s very important!

3. Edit your html file, in this example index.html

<link rel="stylesheet" type="text/css" href="/s/css/grid960.css"/>

4. Copy your resources to “C:\Projects\mydjangoproject\static\css\grid960.css”.

5. python runserver 8080

6. Open browser and visit “http://localhost:8080/%5BYOUR_PAGE%5D&#8221;.

If you notice the step-2 syntax, it is wrapped inside a “if settings.DEBUG” if-block. This variable will be false when you deploy to a production environment where you may have something like an Apache Web server serving all your static resources. So when you move to production, you can push all your static resources to be outside the project.


Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: