How to set the media path to root media folder in Django?

I have a media folder outside the APP, in the project folder (to share media with other apps).

Project structure

Problem:
I'm getting error: "GET /soupiska/pic/hraci/download_6BVC8qn.jpeg HTTP/1.1" 404 2971 but "soupiska" is the APP folder.
The path saved in the database is: pic/hraci/download_6BVC8qn.jpeg

And settings.py includes:


MEDIA_URL = '/media/'

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

The reason for this behavior is, that I'm in APP URL: URL

How to set up Django, to set the media path to a folder in the project folder, even I'm in the APP URL?

Instead of GET /soupiska/pic/hraci/download_6BVC8qn.jpeg

Try GET /pic/hraci/download_6BVC8qn.jpeg

Note: The media files are served independently of the APP Folder.

If you want to strictly use /soupiska in the beginning, then you have to add folder soupiska inside the media folder and add logic to save to that folder inside when media is being saved (mostly in models.py)

To use an Image in a Template, you need the absolute public path to the Image which you can get via the url [djangoproject.com] attribute of the Image. Change this line:

th><img src="{{ hrac.fotka }}"" /></th></code></pre>

To:

<th><img src="{{ hrac.fotka.url }}" /></th>
Вернуться на верх