Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between static files and media files in Django?

Tags:

python

django

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?

like image 337
Silver Light Avatar asked Feb 16 '11 12:02

Silver Light


People also ask

What is static files in Django?

django. contrib. staticfiles provides a convenience management command for gathering static files in a single directory so you can serve them easily. Set the STATIC_ROOT setting to the directory from which you'd like to serve these files, for example: STATIC_ROOT = "/var/www/example.com/static/"

Where are Django static files stored?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.

What are static files?

Static files are files that don't change when your application is running. These files do a lot to improve your application, but they aren't dynamically generated by your Python web server. In a typical web application, your most common static files will be the following types: Cascading Style Sheets, CSS.

What is static URL in Django?

"STATIC_ROOT" sets the absolute path to the folder where static files used for the apps and admin in a django project are stored and this command below creates the folder and collects static files from the apps and admin in a django project into the folder (*Setting "STATIC_ROOT" never ever influence to static file URL ...


2 Answers

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

like image 162
Uku Loskit Avatar answered Oct 18 '22 20:10

Uku Loskit


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.

like image 41
Brian Neal Avatar answered Oct 18 '22 18:10

Brian Neal