Question

I'm trying to serve user uploaded media files in my dev environment.

#settings.py
#[...]
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
MEDIA_ROOT = (os.path.join(SITE_ROOT, 'media/'))
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
  os.path.join(SITE_ROOT, 'static/'),
)
#[...]

#url.py
urlpatterns = patterns('',
  #[...]
  url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], 'django.views.static.serve',
    {'document_root', settings.MEDIA_ROOT}
  ),
  url(r'^%s(?P<path>.*)$' % settings.STATIC_URL[1:], 'django.views.static.serve',
    {'document_root', settings.STATIC_ROOT}
  ),
)

Trying to access an uploded file like http://127.0.0.1:8000/media/videos/julian_06.flv, I get

ValueError at /media/videos/julian_06.flv

dictionary update sequence element #0 has length 40; 2 is required

Was it helpful?

Solution

I'd recommend trying to follow the docs for static hosting in development

if settings.DEBUG:
urlpatterns += patterns('django.contrib.staticfiles.views',
    url(r'^media/(?P<path>.*)$', 'serve'),
)

EDIT:

Your dictionary should have a : not , between the 'document_root' and settings.MEDIA_ROOT

urlpatterns = patterns('',
url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], 'django.views.static.serve',
    {'document_root': settings.MEDIA_ROOT}),
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top