Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Managing Media Files in Django

2 min read

In Django Media files are the files uploaded by users on the system. However, like static files media files shouldn't be trusted.

There are always security concerns when dealing with user-uploaded content. Notably, it’s important to validate all uploaded files. Django offers a large degree of flexibility to manage user uploads.

In this article, we will go through the configuration of media files in Django.

Note that I am assuming you have a basic understanding of Django and have a basic project up and running, if not read the below tutorials.

Configuring Media Files in Django

Open settings.py file of your project and add the following configuration.

# Base url to serve media files
MEDIA_URL = '/media/'

# Path where media is stored'
MEDIA_ROOT = BASE_DIR / 'media'
# Older versions of Django that use os module for path traversal do this instead
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

MEDIA_URL is the URL that will serve the media files.

During development, it doesn't matter much as long it doesn't conflict with any view of the apps. In production, uploaded files should be served from a different domain such as Amazon S3.

MEDIA_ROOT is the path to the root directory where the files are getting stored.

Serving Media Files in Development

By default, Django doesn't serve media files during development( when debug=True).

In order to make the development server serve the media files open the url.py of the project and make the below changes.

url.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    ...]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

That's all now, run the local development server add files in the media root folder and retrieve them from media URL.


DJANGO

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read