pythondjangodjango-staticfilesdjango-static

Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT


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??


Solution

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

    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.