25

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?

Devin Burke
  • 13,642
  • 12
  • 55
  • 82
bash-
  • 6,144
  • 10
  • 43
  • 51

1 Answers1

35

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.

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • thanks for the reply. One more follow up question. What if I got product images? Where should that go? Admins can upload it and should be displayed on site. – bash- Sep 18 '11 at 03:28
  • @bash-: Do you store the paths of product images in the database? The files of [`ImageField`](https://docs.djangoproject.com/en/dev/ref/models/fields/#imagefield) and [`FileField`](https://docs.djangoproject.com/en/dev/ref/models/fields/#filefield) go in `MEDIA_ROOT`, so in this case, yes, product images go in `MEDIA_ROOT` (but you don't need to upload them to `MEDIA_ROOT` by hand—your app should do that). If you are manually maintaining product images and referencing them in `` tags on templates, then the product images should go in `STATIC_ROOT`. – Daniel Trebbien Sep 18 '11 at 11:28