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 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 = 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 of the project and make the below changes.

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

urlpatterns = [
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,

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


Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read