Difference between static STATIC_URL and STATIC_ROOT on Django

Difference between static STATIC_URL and STATIC_ROOT on Django

I am confused by static root and want to clarify things.
To serve static files in Django, the following should be in settings.py and urls.py:
import os
PROJECT_DIR=os.path.dirname(__file__)

1. Absolute path to the directory in which static files should be collected
STATIC_ROOT= os.path.join(PROJECT_DIR,'static_media/')

2. URL prefix for static files
STATIC_URL = '/static/'

3. Additional locations for static files
STATICFILES_DIRS = ( os.path.join(PROJECT_DIR,'static/'),)

...and in urls.py the following lines:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += patterns('', (
    r'^static/(?P.*)$',
    'django.views.static.serve',
    {'document_root': settings.STATIC_ROOT}
))

4. We also use python manage.py collectstatic
Questions:

Could anyone please explain the workflow to me: how should things ideally be done. As of now, I copy/paste the above code snippets into their designated locations and continue making new files in the static directory and it works. In my settings.STATIC_ROOT, however, I have pointed to a different directory. 
It would be great if someone could explain the workflow of each setting: how files are collected and managed, and what would be a good practice to follow. 

Thanks. 

Solutions/Answers:

Answer 1:

STATIC_ROOT

The absolute path to the directory where ./manage.py collectstatic will collect static files for deployment.
Example: STATIC_ROOT="/var/www/example.com/static/"

now the command ./manage.py collectstatic will copy all the static files(ie in static folder in your apps, static files in all paths) to the directory /var/www/example.com/static/. now you only need to serve this directory on apache or nginx..etc.

STATIC_URL

The URL of which the static files in STATIC_ROOT directory are served(by Apache or nginx..etc).
Example: /static/ or http://static.example.com/

If you set STATIC_URL = 'http://static.example.com/', then you must serve the STATIC_ROOT folder (ie "/var/www/example.com/static/") by apache or nginx at url 'http://static.example.com/'(so that you can refer the static file '/var/www/example.com/static/jquery.js' with 'http://static.example.com/jquery.js')

Now in your django-templates, you can refer it by:

{% load static %}
<script src="{% static "jquery.js" %}"></script>

which will render:

<script src="http://static.example.com/jquery.js"></script>

Answer 2:

STATICFILES_DIRS: You can keep the static files for your project here e.g. the ones used by your templates.

STATIC_ROOT: leave this empty, when you do manage.py collectstatic, it will search for all the static files on your system and move them here. Your static file server is supposed to be mapped to this folder wherever it is located. Check it after running collectstatic and you’ll find the directory structure django has built.

——–Edit—————-

As pointed out by @DarkCygnus, STATIC_ROOT should point at a directory on your filesystem, the folder should be empty since it will be populated by Django.

STATIC_ROOT = os.path.join(BASE_DIR, ‘staticfiles’))

or

STATIC_ROOT = ‘/opt/web/project/static_files

——–End Edit —————–

STATIC_URL: ‘/static/’ is usually fine, it’s just a prefix for static files.

Answer 3:

All the answers above are helpful but none solved my issue. In my production file, my STATIC_URL was https://<URL>/static and I used the same STATIC_URL in my dev settings.py file.

This causes a silent failure in django/conf/urls/static.py.

The test elif not settings.DEBUG or '://' in prefix:
picks up the ‘//’ in the URL and does not add the static URL pattern, causing no static files to be found.

It would be thoughtful if Django spit out an error message stating you can’t use a http(s):// with DEBUG = True

I had to change STATIC_URL to be ‘/static/’

References