5

In the documentation https://docs.djangoproject.com/en/dev/howto/static-files/

I read that static files should be put with their respective apps and called upon with

{% load staticfiles %}
<img src="{% static "articles/css/base.css" %}" alt="My image"/>

However later on in the docs it mentions that some static files don't pertain to a particular app. This is where STATICFILES_DIRS comes into play. If I read correctly STATICFILES_DIRS is a tuple for Django to use to look for other static files. I was wondering how would I call the static files that was called from the STATICFILES_DIRS?

ex: something like

<link rel="stylesheet" type="text/css" href="{% static "/css/default.css" %}">

Also I am not sure what to put for my STATIC_ROOT. Do I leave it empty? ('')

My proj tree

mysite
  \articles
       \static
       \articles
           \css
               base.css
  \static
       \images
       \css
           default.css
       \js 
  \templates
       base.html
  \settings.py 

This is currently in my settings.py regarding static files

# looks for static files in each app
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATICFILES_STORAGE = (
    'django.contrib.staticfiles.storage.StaticFilesStorage'
)

# the absolute path to the directory where collectstatic will collect static files for deployment (OUTPUT)
STATIC_ROOT = ''

# This setting defines the additional locations the static files app will traverse if the FileSystemFinder finder is enabled.
STATICFILES_DIRS = (
    # used for static assets that aren't tied to a particular app
     os.path.join(BASE_DIR, 'static'),
)

# URL to use when referring to static files located in STATIC_ROOT
STATIC_URL = '/static/'
Liondancer
  • 15,721
  • 51
  • 149
  • 255

1 Answers1

3

Almost everything about django static is related to the django.contrib.staticfiles app. Although you need to custom edit many settings to make staticfiles work, what is does is simple. It provides a collectstatic command which collects static files from different app and put them into a single directory.

The answer to your first question is simple: Put those common static files under the /static directory of your django project directory. In your case, it's mysite/static.

Reason: First, it's the official way. You can find the following code in official doc: Managing static files (CSS, images). Second, it's reasonable. Since we put static files only used in a single app under project/appnane/static/... The project's static dir should follow the same name pattern.

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"), # That's it!!
    '/var/www/static/',
)

As I said in the comment, your should not set STATIC_ROOT to project_absolutr_path/static. Because that directory is user to put css app static files. You don't want the collectstatics command to pollute that directory especially when you are using a version control system like git/svn.

STATIC_ROOT really depends on the way you host these static files(Apache, Nginx, S3, CDN, Paas like heroku)

Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38
  • I appreciate your effort! It seems to me that `STATIC_ROOT` does depend on the situation. What if I were to host these files on heroku? – Liondancer Apr 10 '14 at 03:39
  • On heroku, you don't have access to apache/nginx, so have to either host static with django itself, or use a cdn. – Leonardo.Z Apr 10 '14 at 03:44
  • They have a detailed doc about it https://devcenter.heroku.com/articles/django-assets – Leonardo.Z Apr 10 '14 at 03:45
  • I am just confused because I still don't know what to input for `STATIC_ROOT` because as you said it depends on the situation. – Liondancer Apr 10 '14 at 03:51
  • 1
    If you use django itself to host static files on heroku, it's reasonable to put the collected files under a directory of your project (not 'static'). So you could set STATIC_ROOT to `staticfiles`(or 'asserts', ). The link I posted (https://devcenter.heroku.com/articles/django-assets) mentioned that. – Leonardo.Z Apr 10 '14 at 05:07