Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT

56,936

Solution 1

You can find these settings in the Django documentation. Here are my own definitions and quotations from the documentation:

  • MEDIA_ROOT is the folder where files uploaded using FileField will go.

    Absolute filesystem path to the directory that will hold user-uploaded files.

  • STATIC_ROOT is the folder where static files will be stored after using manage.py collectstatic

    The absolute path to the directory where collectstatic will collect static files for deployment.

    If the staticfiles contrib app is enabled (default) the collectstatic management command will collect static files into this directory. See the howto on managing static files for more details about usage.

  • STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.

    This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.

In your settings, you should have:

MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

# Make a tuple of strings instead of a string
STATICFILES_DIRS = ("/home/user/project/django1/top/listing/static", )

...where:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

as defined in the default Django settings.py now.

Solution 2

Development

STATIC_ROOT is useless during development, it's only required for deployment.

While in development, STATIC_ROOT does nothing. You don't even need to set it. Django looks for static files inside each app's directory (myProject/appName/static) and serves them automatically.

This is the magic done by manage.py runserver when DEBUG=True.

Deployment

When your project goes live, things differ. Most likely you will serve dynamic content using Django and static files will be served by Nginx. Why? Because Nginx is incredibly efficient and will reduce the workload off Django.

This is where STATIC_ROOT becomes handy, as Nginx doesn't know anything about your Django project and doesn't know where to find static files.

So you set STATIC_ROOT = '/some/folder/' and tell Nginx to look for static files in /some/folder/. Then you run manage.py collectstatic and Django will copy static files from all the apps you have to /some/folder/.

Extra directories for static files

STATICFILES_DIRS is used to include additional directories for collectstatic to look for. For example, by default, Django doesn't recognize /myProject/static/. So you can include it yourself.

Example

STATIC_URL = '/static/'

if not DEBUG:
    STATIC_ROOT = '/home/django/www-data/site.example/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static/'),
]

Solution 3

Difference between STATICFILES_DIRS and STATIC_ROOT

The STATICFILES_DIRS can contain other directories (not necessarily app directories) with static files and these static files will be collected into your STATIC_ROOT when you run collectstatic. These static files will then be served by your web server and they will be served from your STATIC_ROOT.

If you have files currently in your STATIC_ROOT that you wish to serve then you need to move these to a different directory and put that other directory in STATICFILES_DIRS. Your STATIC_ROOT directory should be empty and all static files should be collected into that directory.

MEDIA_ROOT where media files ,all uploaded files goes. Example : Images, Files

Share:
56,936

Related videos on Youtube

dev-jim
Author by

dev-jim

A Coder...

Updated on July 08, 2022

Comments

  • dev-jim
    dev-jim almost 2 years

    What are the differences of these three static url?

    I am not sure if I am right, I am using the MEDIA_ROOT to store my uploaded photos (via models.ImageField())

    However, I created a JS script to my admin and in admin.py. I defined the media as below:

    ....
    class Media:
          js = ('/admin/custom.js', )
    

    and my settings.py:

     ....
     STATIC_ROOT = "/home/user/project/django1/top/listing/static"
    

    and I added the custom.js to STATIC_ROOT/admin/custom.js, but it is not working. Throwing 404 not found error.

    And then I change the STATIC_ROOT to STATICFILES_DIRS, and it works!!

    ....
    STATICFILES_DIRS = "/home/user/project/django1/top/listing/static"
    

    So, I am not understand what is going on here. In fact, I just don't understand what is the difference between STATIC_ROOT and STATICFILES_DIRS.

    Currently I am testing Django in my machine via virtualenv, not deployed yet, is it the reason STATIC_ROOT not working??

  • Ian Clark
    Ian Clark almost 10 years
    This is easily the clearest answer IMO, though I feel like this question is a definite dupe
  • dev-jim
    dev-jim almost 10 years
    Thanks for the answer. Wish that I can accept more than 1 answer for the question. Your answer also very helpful to me.
  • Homunculus Reticulli
    Homunculus Reticulli almost 7 years
    Best explanation for STATIC_ROOT etc. Clear and concise. This is what should be in the django documentation.
  • Eje
    Eje almost 6 years
    @MaximeLorant Honestly, your explanation is better than the Django docs.
  • adhg
    adhg over 5 years
    @Max Malysh that's the level of answer we should have. Got it! A+
  • MadPhysicist
    MadPhysicist almost 5 years
    Much elucidating. Thanks!
  • Llanilek
    Llanilek over 4 years
    @MaximeLorant beautifully constructed answer. As Jee mentioned, much clearer than the docs themselves.
  • JMJ
    JMJ over 2 years
    Thanks! This clear answer is much better than the confusing text in the Django docs.
  • Konstantin Kozirev
    Konstantin Kozirev over 2 years
    You don't even have to add STATICFILES_DIRS (if you don't have additional directories to serve file from), you can add static(...) function to urlpatterns in urls.py, which as stated in docs will work only in Debug mode.