Update 2009-03-25

I realize why this isn’t needed. If your production environment is set up correctly, django will never serve the static urls and you aren’t running the django development server anyways, so having django.views.static.serve in your urls file won’t hurt anything. But it is nice to have if multiple people will be developing this on different machines.

Original Article

For developing a django application, I’ve been using the webserver that comes with django. It’s great for development since there is no need to restart apache. The quick and easy way to server a static file with the django webserver is to use this in your urls.py:

urlpatterns = patterns('',
    (r'^files/(?P<path>.*)$',
        'django.views.static.serve',
        {'document_root': '/home/django-proj/files'}
    ),
# ... more url patterns
)

Which worked great for testing things out, but it is definitely not a good idea to use this in a production environment. On the production server I set apache to not bother with mod_python for those directories so that django doesn’t have to do any processing (here is a thorough example of using lighttpd to serve static files).

Since I wanted to use the same files on each server, I didn’t want the static serve in the url patterns on production. To add more patterns to urlpatterns, you can do urlpatters += patterns(”, to add more. I also added the file locations into settings.py since they would be different for other developers. Using if hasattr(settings, ‘STATIC_FILE_LOCATION’): I was able to keep the urlpatterns in the right format whether it’s in a production or development environment.

An example of what I’m using:

from django.conf.urls.defaults import *
from django.conf import settings
 
# basic urls
urlpatterns = patterns('',
    (r'^logout/', 'user.views.logout'),
    (r'^login/', 'user.views.login'),
)
 
# if on a development machine
if hasattr(settings, 'STATIC_FILE_LOCATION'):
    urlpatterns += patterns('',
        (r'^files/(?P<path>.*)$',
        'django.views.static.serve',
        {'document_root':
        settings.STATIC_FILE_LOCATION}),
    )
 
# application urls
urlpatterns += patterns('',
    # add new applications below here
    (r'^user/', include('user.urls')),
    # end applications
 
    # the index
    (r'^$', 'user.views.index'),
)

I use webfaction to host a lot of my django projects. It has an easy setup that will get you developing quickly and a great community of talented programmers. There is also a quick setup for rails, wordpress, and a lot more.

Related posts:

  1. Python Projects in Users’ Home Directories with wsgi  Letting users put static files and php files in a public_html folder in their home directory has been a common...
  2. How to Speed up Your Django Sites with NginX, Memcached, and django-compress  A lot of these steps will speed up any kind of application, not just django projects, but there are a...
  3. How to Write Reusable Apps for Pinax and Django   Pinax is a collection of reusable django apps that brings together features that are common to many websites. It...