问题
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