user3475724
user3475724

Reputation:

Media files don't work in template on Django localhost

Trying to set up media locally. In template generated path appears to be correct and looks like this /media/event_img/1058

<div style="height: 201px; background: url('{{ event.image.url }}')  no-repeat; background-size: cover;background-color: #091026;"></div>

But still, no picture is shown(It exists for every item, I checked manually), and if enter http://localhost:8000/media/event_img/1058 I'll just get 404 error, URL pattern not found.

In settings.py :

MEDIA_ROOT = 'files/media/'

MEDIA_URL = '/media/'

What may be wrong? Where to look for the issue? As I understand to the documentation this setup is enough to output media files.

Upvotes: 0

Views: 124

Answers (1)

Sachin
Sachin

Reputation: 3674

Provide the valid full path to the media directory.

In settings.py:

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

In urls.py:

urlpatterns = [
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This requires a directory named files where manage.py is, in which is containing directory media and in it will be the uploaded files.

Ref: MEDIA_ROOT docs in Django 1.11

Upvotes: 1

Related Questions