Serving Static Files in Django

Note to Public Visitors

This article and its contained information are intended as additional information for myself on top of what can be found on the internet in general. By no means it is covering the topic exhaustingly.

Software Versions in this How-To

  • Ubuntu 10.04
  • Apache 2.2.14
  • Python 2.6
  • Django 1.15

Resources

dev server

  • https://docs.djangoproject.com/en/1.5/howto/static-files/

production server

  • https://docs.djangoproject.com/en/1.5/howto/static-files/deployment/
  • https://docs.djangoproject.com/en/1.5/howto/deployment/wsgi/modwsgi/#serving-files

Information

  • STATIC_ROOT = '/static/'

    will serve the static files from the domains root path where as

    STATIC_ROOT = 'static/'

    will serve the static files from the path that was requested via the url plus a “static” segment

  • The contents of the variable STATICFILES_FINDERS
    STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    #'django.contrib.staticfiles.finders.DefaultStorageFinder',
    )
    

    determine the following:

    • how (and thus where) the dev server will search for static files when it wants to serve them
    • how (and thus where) django will search for static files when the shell-command
      python manage.py collectstatic
      

      is used.

  • When
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    

    is used in variable STATICFILES_FINDERS

    • the static files have to reside in a subfolder called ‘static/’ within an apps folder (eg: MyProject/App1/static/)
    • no static files will be collected from the projects own packageĀ folder (eg: MyProject/MyProject/static/)

Dos and Don’t Dos

  • when using the dev server for a project, in any case use
    STATIC_ROOT = '/static/'
    • because the dev server serves the website at the root of ‘localhost:<port>’
    • and the static files being served at ‘localhost:<port>/static/’ fits this behavior.

How Tos

Serving a Django Project from an URL-Path on a Production Server

Important Note

This howto just contains the differences in the setup from the setup of django project/a website in a domains root path.

Basic Explanation of how it works

The shell command

python manage.py collectstatic

will be used to collect all static files once to a folder, from where Apache will serve them later on. In Apaches config file an alias has to be set, which maps the the URL-path from where Apache serves the static files to the folder on the server.

Configuration of the Django Project

settings.py

  • Set STATIC_URL to the absolute url-path from where the static files for this project shall be served (eg.: /project89/static/).
  • Set STATIC_ROOT to the folder on the server where the static files (will be collected to and will be served from by apache) shall reside, using an absolute path (eg: /home/user/djangoprojects/project89/collectedstaticfiles/).

Configuration of Apache

httpd.conf

Note that any changes to httpd.conf require a restart of apache.

  • The alias config string that maps the url-path of the static files to a folder on the server has to include the full path in the url
    eg:

    Alias /project89/static/ /home/user/djangoprojects/project89/collectedstaticfiles/
    

Comments are closed.