79

I'm moving to Django 1.3 and find this separation of media and static files a bit confusing. Here is how default settings.py looks like:

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''

# Absolute path to the directory that holds static files.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''

# URL that handles the static files served from STATIC_ROOT.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

What should I put into MEDIA_ROOT and a STATIC_ROOT? Should those be separate directories? What is the difference?

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
Silver Light
  • 44,202
  • 36
  • 123
  • 164

3 Answers3

82

Static files are meant for javascript/images etc, but media files are for user-uploaded content.

alex ferra
  • 13
  • 3
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • what about apps that still store their stuff into media. for example contrib.admin. should i be symlinking to my projects media folder or static folder now? because according to your're answer those files now belong in static. – darren Apr 20 '11 at 09:38
  • @mongoose_za - the admin app now has a static folder and no longer uses media. You no longer need to symlink admin's media files if you use the staticfiles app. – Brian Neal Nov 17 '11 at 13:47
42

As Uku Loskit said, static files are for things like your applications' css files, javascript files, images, etc. Media files are typically user or admin uploadable files.

Normally you will want MEDIA_ROOT and STATIC_ROOT to be separate directories. Keep in mind that STATIC_ROOT is where the management command collectstatic will place all the static files it finds. In production, you then configure your webserver to serve the files out of STATIC_ROOT when given a request that starts with STATIC_URL. If you are using the Django devserver for development, it will automatically serve static files.

The staticfiles application thus disentangles user uploaded media from application media, thus making deployment, backups, and version control easier. Prior to the staticfiles app, it was common for developers to have the media files mixed in with static application assets.

The 1.3 docs for staticfiles have been steadily improving; for more details, look at the how-to.

Brian Neal
  • 31,821
  • 7
  • 55
  • 59
0

Static files:

Used for application assets such as CSS, and Javascript to design the UI of the application. Static files are expected to unchanged during the application execution. It is a common practice in Django to keep all the static files in the directory static.

All the files stored in this directory can be called using the command 'collectstatic', an efficient way to call the files in Django.

Media files:

These are user-generated, applications specific such as images, documents, video, audio files, etc. Media files are generally unique to each user or application and may change frequently.

Media files are stored in the directory media in Django. Unlike static files, media files are not collected or managed by Django. Instead, Django provides utilities to handle media file uploads and serves them directly from the specified media URL.

Ravi
  • 1
  • 3
  • Given some of your other answers over the last few days, this one also appears likely to have been written (entirely or partially) by AI (e.g., ChatGPT). A heads-up that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. – NotTheDr01ds Jul 13 '23 at 11:54