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

  • Recent Posts

    August 2017
    M T W T F S S
    « Jul    
  • Subscribe Options

  • Awards

  • Most Valuable Blogger @ DZone
  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 173 other followers

  • Follow MyThinkPond on
  • Blog Stats

    • 364,698 hits
  • General Options

Archive for the ‘Django’ Category

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.

Posted in Django, Python | Tagged: , , , , | Leave a Comment »