我在此问题Django: Adding files to a form gives multiple argument error
中有详细信息如何下载上传的文件。当我去编辑表单的视图时,我可以看到上传的文件网址,但它没有下载。 开发和生产模式需要更改哪些设置?
点击链接时出错:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/Certificate.docx
Using the URLconf defined in tiktant.urls, Django tried these URL patterns, in this order:
^ ^$ [name='home']
^ ^login/$ [name='login']
^ ^logout/$ [name='logout']
^ ^logout_then_login/$ [name='logout_then_login']
^ ^dashboard/$ [name='dashboard']
The current URL, media/Certificate.docx, didn't match any of these.
答案 0 :(得分:4)
根urls.py文件中未显示媒体网址。您需要在urls.py文件中添加以下代码以启用相同的功能。
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
注意:请不要忘记在settings.py文件中添加MEDIA_ROOT和MEDIA_URL。要下载文件,您需要编写更多代码。
答案 1 :(得分:0)
确保在项目中添加以下行 settings.py
import os
def root(x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',x)
MEDIA_ROOT = root('media')
MEDIA_URL = '/media/'
TEMPLATE_CONTEXT_PROCESSORS = (
'-----------------------'
'django.core.context_processors.media',
)
urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
urlpatterns += patterns('',url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)