Login

Serve from STATIC_ROOT

Author:
popen2
Posted:
May 18, 2011
Language:
Python
Version:
1.3
Tags:
staticfiles STATIC_ROOT
Score:
-1 (after 1 ratings)

Assuming you have defined STATIC_ROOT correctly in settings.py, and have installed staticfiles_urlpatterns() in urls.py (as demoed in the code, taken from Django's staticfiles docs), the code from static_root_finder.py can be used to simply serve static files in development from STATIC_ROOT directly, avoiding complicated setups in case we just want to have a bunch of static files in one directory.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#------------- static_root_finder.py -------------------#

from django.contrib.staticfiles.finders import BaseFinder
from django.conf import settings
import os

class StaticRootFinder(BaseFinder):
    def __init__(self, apps=None, *args, **kwargs):
        super(StaticRootFinder, self).__init__(*args, **kwargs)

    def find(self, path, all=False):
        """
        Looks for files in the extra locations
        as defined in ``STATIC_ROOT``.
        """
        abs_path = os.path.join(settings.STATIC_ROOT, path)
        if os.path.exists(abs_path):
            return abs_path if not all else [abs_path]
        return []

#------------- urls.py -------------------#

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
#
# Your urls.py file
#
urlpatterns += staticfiles_urlpatterns()


#------------- settings.py -------------------#

STATICFILES_FINDERS = (
    'static_root_finder.StaticRootFinder',
)

More like this

  1. Serve static media and indexes from app directories [Python2.5, Development only] by adamlofts 6 years, 10 months ago
  2. Debug-only static serving by ludvig.ericson 7 years, 6 months ago
  3. Custom mod_python AuthenHandler by aeby 7 years, 11 months ago
  4. X-Sendfile static file serve view by dokterbob 4 years, 9 months ago
  5. Serve static media files from app/media subdirectory by adamlofts 6 years, 11 months ago

Comments

Please login first before commenting.