Django Media Files
Learn how to manage MEDIA files in Django
This page explains how to manage MEDIA files in Django by coding all the steps.
✅ Step #1 - Create the MEDIA folder in the root of the project
$ mkdir media
✅ Step #2 - Update Project Settngs - Just add the below code at the end
# MEDIA Files
MEDIA_URL = '/media/'
# Path where media is stored
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
✅ Step #3 - Update Routing (
urls.py
)
# New imports
from django.conf import settings
from django.conf.urls.static import static
# The current rules
urlpatterns = [ # no changes here
... # no changes here
path("admin/", admin.site.urls), # no changes here
... # no changes here
] # no changes here
# new rules
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
This update is necessary because Django is not managing the MEDIA
folder in development.
✅ Step #4 - Save a file in the media folder
$ cp <SOURCE>/1.jpg media/1.jpg
Step #5 - Use the media file in
view
- Simply use the mediaPATH
<section class="min-vh-100 d-flex align-items-center section-image overlay-soft-dark"
data-background="/media/1.jpg"> <!-- MEDIA file isused here -->
<div class="container">
<!-- -->
</div>
</section>
At this point, the media file should be visible in the UI.
✅ Resources​
- 👉 Access AppSeed for more starters and support
- 👉 Deploy Projects on Aws, Azure and DO via DeployPRO
- 👉 Create landing pages with Simpllo, an open-source site builder
- 👉 Build apps with Django App Generator (free service)