4

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.

Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180

1 Answers1

14

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)
Andreas
  • 1,091
  • 1
  • 11
  • 16
  • Actually there is no need to check `settings.DEBUG`, because `static()` already does that for you. You can see this in the [source](https://github.com/django/django/blob/stable/3.2.x/django/conf/urls/static.py#L23) and [docs](https://docs.djangoproject.com/en/stable/ref/urls/#static). – djvg Apr 12 '21 at 09:00