Serve from STATIC_ROOT

 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 4 years, 10 months ago
  2. collectstatic for media folders by pterk 2 years, 2 months ago
  3. Better Static Image Serving With Fallback by menendez 3 years, 3 months ago
  4. Routing urls.py By Convention by doconix 1 year ago
  5. Coffeescript compilation by delfick 4 months, 3 weeks ago

Comments

jezdez (on May 18, 2011):

This is ass-backward, STATIC_ROOT isn't supposed to be served directly in development mode.

Just follow the basic usage docs and add a separate (different from STATIC_ROOT) directory to STATICFILES_DIRS to serve it (e.g for 'project' files).

#

(Forgotten your password?)