user2472819
user2472819

Reputation: 123

Django - Loading Images From Folders Within Media Folder

I'm having an issue where I can't load images from anywhere but my media folder.

For example:

<img src="media/image.png" />

will load just fine

however if i move the "image.png" file 1 more folder deeper to:

<img src="media/folder/image.png" />

"image.png" will not load.

Does anyone have any ideas as to why this is happening?

Upvotes: 4

Views: 3758

Answers (2)

user1953366
user1953366

Reputation: 1611

If not in production then perhaps this would be helpful: https://stackoverflow.com/a/52672594/1953366

It worked for me. You can use "/" for PATH to directly map the files. Folder hierarchy is not exposed though.

Upvotes: 0

Pawel Miech
Pawel Miech

Reputation: 7822

You need to use django staticfiles module. 'Static' figures out where your files are placed and redirects all requests to this folder

Assuming that your image.png is located in you/yourapp/static/media/folder the following should do.

{% load  static %}
<img src="{% static 'media/folder/image.png' %}" />

Read the docs about serving static files with django.

Upvotes: 2

Related Questions