Configure static resources in Django - Python | MyThinkPond

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:

[sourcecode language=“jscript”] STATIC_ROOT = “ STATIC_URL = ‘/s/’ STATICFILES_DIRS = ( ‘C://Projects//mydjangoproject//static//‘, ) [/sourcecode]

  1. Edit “” and add the following:

[sourcecode language=“jscript”] from spog import settings … if settings.DEBUG: urlpatterns += patterns(“, (r’^s/(?P.*)$‘, ‘django.views.static.serve’, {‘document_root’: settings.STATIC_ROOT}), ) [/sourcecode]

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

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

[sourcecode language=“html”]


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

  2. python runserver 8080

  3. Open browser and visit “http://localhost:8080/[YOUR_PAGE]".

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.

Venkatt Guhesan avatar
About Venkatt Guhesan
Enterprise Software Architect currently working at DataDirect Networks developing a highly scalable monitoring solution called DDN Insight. My passions include all things Open Source including Linux (CentOS, Red Hat, Gentoo, Open Stack, Docker & Ubuntu), embedded ARM processors and microcontrollers (Arduino, UDOO, Cubieboard, BeagleBoard, OnionIO, TI's Zigbee). Coming from an Electrical and Computer Engineering background, working in developing and designing enterprise - distributed, highly scalable software requires a unique set of software skills with a good understanding of the hardware. This makes his work challenging and interesting. In his free time, he spends his time with his family and two children. He also enjoys researching on topics including - world ancient history and yoga. Visit the 'About' section for more details.
comments powered by Disqus