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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.