Difference between static STATIC_URL and STATIC_ROOT in Django

I am confused by static root and I want to clarify the situation.

To serve static files in Django, the urls.py should be specified in settings.py and urls.py :

 import os PROJECT_DIR=os.path.dirname(__file__) 

1. The absolute path to the directory where 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 places for static files

 STATICFILES_DIRS = ( os.path.join(PROJECT_DIR,'static/'),) 

... and in urls.py following lines:

 from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns += patterns('', ( r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT} )) 

4. We also use python manage.py collectstatic

Questions:

  • Can someone explain to me the workflow: how everything should be ideal. At the moment, I copy / paste the above code snippets to their designated places and continue to create new files in the static directory, and it works. However, in my settings.STATIC_ROOT I pointed to a different directory.

  • It would be great if someone could explain the workflow of each parameter: how the files are assembled and managed, and what would be good practice.

Thank.

+105
django django-urls django-staticfiles
Dec 31 '11 at 11:49
source share
3 answers

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 ./manage.py collectstatic command will copy all static files (i.e. to a static folder in your applications, static files in all ways) to the /var/www/example.com/static/ directory. now you only need to maintain this directory on apache or nginx..etc.

STATIC_URL

URL that serves static files in STATIC_ROOT (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 (that is, "/var/www/example.com/static/" ) via apache or nginx at the URL 'http://static.example.com/' (so you can link to the static file '/var/www/example.com/static/jquery.js' using 'http://static.example.com/jquery.js' )

Now in your django templates you can reference this:

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

which will do:

 <script src="http://static.example.com/jquery.js"></script> 
+69
Sep 05 '13 at 6:09 on
source share

STATICFILES_DIRS : Here you can store static files for your project, for example, those used by your templates.

STATIC_ROOT : leave this blank when you manage.py collectstatic , it will search for all static files in your system and move them here. Your static file server should be tied to this folder, wherever it is. Check this out after running collectstatic and you will see the directory structure created by django.

--------Edit----------------

As @DarkCygnus points out, STATIC_ROOT should point to a directory in your file system, the folder should be empty, as it will be filled with Django.

STATIC_ROOT = os.path.join (BASE_DIR, 'staticfiles'))

or

STATIC_ROOT = '/ opt / web / project / static_files

''

-------- End Edit -----------------

STATIC_URL : '/ static /' is usually ok, it's just a prefix for static files.

+27
Dec 31 '11 at 19:05
source share

All of the answers above are helpful, but no one solved my problem. In my working file, my STATIC_URL was https://<URL>/static and I used the same STATIC_URL in my dev settings.py file.

This results in a silent error in django / conf / urls / static.py.

The elif not settings.DEBUG or '://' in prefix: test selects "//" in the URL and does not add a static URL pattern, so static files are not detected.

It would be helpful if Django posted an error message stating that you cannot use http(s):// with DEBUG = True

I had to change STATIC_URL to "/ static /"

0
Jan 13 '19 at 17:23
source share



All Articles