我正在尝试在我的开发环境中提供用户上传的媒体文件。
#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}
),
)
尝试访问像http://127.0.0.1:8000/media/videos/julian_06.flv这样的上传文件,我得到了
/media/videos/julian_06.flv上的ValueError
字典更新序列元素#0的长度为40; 2是必需的
答案 0 :(得分:1)
我建议您尝试按照static hosting in development
的文档进行操作if settings.DEBUG:
urlpatterns += patterns('django.contrib.staticfiles.views',
url(r'^media/(?P<path>.*)$', 'serve'),
)
修改强>
您的词典在:
和,
之间应该有 'document_root'
而不是 settings.MEDIA_ROOT
urlpatterns = patterns('',
url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)