Login

Serve from STATIC_ROOT

Author:
popen2
Posted:
May 18, 2011
Language:
Python
Version:
1.3
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. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.