Django. Media Files not found

爱⌒轻易说出口 提交于 2019-12-19 11:58:38

问题


Ok, I have a Django 1.10 project. The relevant settings look like this:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = BASE_DIR + "/media/"
MEDIA_URL = '/media/'

I'm working locally, I can upload images correctly. But when I try to access the image on a template using {{ image.image.url }}, I get a 404. In the terminal I can see this:

[06/Sep/2016 18:13:43] "GET /media/folder/uploaded_image.jpg HTTP/1.1" 404 4900

But if I look into my folder, the file is there, correctly uploaded by django.


回答1:


Try using os.path.join, like this:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

You probably also need to update your urls.py with this:

from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

if settings.DEBUG is True:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


来源:https://stackoverflow.com/questions/39359104/django-media-files-not-found

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