Django: Downloading uploaded files

家住魔仙堡 提交于 2019-12-07 00:09:34

The media url is not showing in the root urls.py file. You need to add the below code in the urls.py file to enable the same.

if settings.DEBUG:
    from django.conf.urls.static import static
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

Note: Please don't forget to add the MEDIA_ROOT and MEDIA_URL in settings.py file. For downloading a file, you need to write some more code.

make sure you added the following lines in your project 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}),

)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!