2

Duplicate of Django staticfiles app help

I'm using Django 1.3 beta and the static files app is confusing. In development mode it is meant to automatically serve files from the STATIC_URL path.

From http://docs.djangoproject.com/en/dev/howto/static-files/

If you're using the built-in development server (the runserver management command) and have the DEBUG setting set to True, your staticfiles will automatically be served from STATIC_URL in development.

This didn't seem to work, so I tried a url pattern ('/static/') which routes to the static.serve view. This just 404'd. Somehow it conflicts with the STATIC_URL, if I change it to 'assets/' it will serve the files from static just fine. It's only logical to use '/static' for the static url, but this conflicts.

Url Patterns:

urlpatterns = patterns('',
    # Serve static files for *development only*
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.STATIC_ROOT}),

Static files settings:

STATIC_ROOT = '/home/dave/static/flux'

# URL that handles the static files served from STATIC_ROOT.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

Ideally I would like Django to use the static url for seving files in development without having to use any urlpatterns.

Community
  • 1
  • 1
Benbob
  • 13,876
  • 18
  • 79
  • 114
  • 1
    possible duplicate of [django staticfiles app help](http://stackoverflow.com/questions/4565935/django-staticfiles-app-help) – Benbob Jan 19 '11 at 01:28
  • Go ahead and vote to close/delete your own question then. – Paul McMillan Feb 03 '11 at 03:18
  • I don't have the reputation required to do that. I've voted to close but it needs more votes. I think I've made it clear enough that it's a duplicate to avoid wasting peoples time as much as I can. – Benbob Feb 05 '11 at 09:40

1 Answers1

5

If you want to serve up the static files while using the built in Django server you will need to add a urlpattern. This is what I do (add this after all your other patterns:

if settings.DEBUG:
    urlpatterns += patterns('',
            (r'^static/(.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_PATH}),
    )
MattoTodd
  • 14,467
  • 16
  • 59
  • 76