Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django - when should I use media_root or static_root?

Tags:

I'm confused about static files and media files in django. I've seen elsewhere that people use it interchangeably.

When should I use media_root and when should I use static_root?

If I have site images should I put it in static? And if I have product images do I put it in media?

like image 857
bash- Avatar asked Sep 17 '11 18:09

bash-


People also ask

What is static URL and static root in Django?

STATIC_ROOT is the single root directory from where the Django application will serve the static files in production. The command manage.py collectstatic will automatically compile all the static files throughout the project and dump it into a single root directory, which is declared in STATIC_ROOT .

What is load static in Django?

Static Files such as Images, CSS or JS files are often loaded via a different app in production websites to avoid loading multiple stuff from the same server.

What is static and media in Django?

First off, you'll typically find these three types of files in a Django project: Source code: These are your core Python modules and HTML files that make up every Django project, where you define your models, views, and templates. Static files: These are your CSS stylesheets, JavaScript files, fonts, and images.


1 Answers

MEDIA_ROOT is the directory where file uploads are placed, as well as where generated files are usually stored. For example, one of my Django apps allows users to upload images. In one of the model classes, I use the ImageField type from sorl-thumbnail with upload_to='%Y-%m'. Whenever a user uploads an image, the file is stored in MEDIA_ROOT/%Y-%m/ (with %Y replaced with the current year and %m replaced with the current month number). Also, when sorl-thumbnail generates a thumbnail for an uploaded image, it places the thumbnail by default somewhere in MEDIA_ROOT/cache/.

STATIC_ROOT is used to configure the directory where static assets are placed. For example, site stylesheets, JavaScript files, and images used in the design of web pages are the types of files that go into STATIC_ROOT. If you have multiple installed apps, each app that uses static files can have its own static files directory. You use the collectstatic management function (invoked via python manage.py collectstatic) to copy all of the apps' static files into STATIC_ROOT.

like image 75
Daniel Trebbien Avatar answered Dec 02 '22 11:12

Daniel Trebbien