我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.conf.settings.STATICFILES_DIRS。
def __init__(self, app_names=None, *args, **kwargs): # List of locations with static files self.locations = [] # Maps dir paths to an appropriate storage instance self.storages = OrderedDict() if not isinstance(settings.STATICFILES_DIRS, (list, tuple)): raise ImproperlyConfigured( "Your STATICFILES_DIRS setting is not a tuple or list; " "perhaps you forgot a trailing comma?") for root in settings.STATICFILES_DIRS: if isinstance(root, (list, tuple)): prefix, root = root else: prefix = '' if settings.STATIC_ROOT and os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root): raise ImproperlyConfigured( "The STATICFILES_DIRS setting should " "not contain the STATIC_ROOT setting") if (prefix, root) not in self.locations: self.locations.append((prefix, root)) for prefix, root in self.locations: filesystem_storage = FileSystemStorage(location=root) filesystem_storage.prefix = prefix self.storages[root] = filesystem_storage super(FileSystemFinder, self).__init__(*args, **kwargs)
def __init__(self, apps=None, *args, **kwargs): # List of locations with static files self.locations = [] # Maps dir paths to an appropriate storage instance self.storages = SortedDict() if not isinstance(settings.STATICFILES_DIRS, (list, tuple)): raise ImproperlyConfigured( "Your STATICFILES_DIRS setting is not a tuple or list; " "perhaps you forgot a trailing comma?") for root in settings.STATICFILES_DIRS: if isinstance(root, (list, tuple)): prefix, root = root else: prefix = '' if os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root): raise ImproperlyConfigured( "The STATICFILES_DIRS setting should " "not contain the STATIC_ROOT setting") if (prefix, root) not in self.locations: self.locations.append((prefix, root)) for prefix, root in self.locations: filesystem_storage = FileSystemStorage(location=root) filesystem_storage.prefix = prefix self.storages[root] = filesystem_storage super(FileSystemFinder, self).__init__(*args, **kwargs)
def find(self, path, all=False): """ Looks for files in the extra locations as defined in ``STATICFILES_DIRS``. """ matches = [] for prefix, root in self.locations: if root not in searched_locations: searched_locations.append(root) matched_path = self.find_location(root, path, prefix) if matched_path: if not all: return matched_path matches.append(matched_path) return matches
def generate_manifest(is_server_live: bool, bundle_path: str, app_dir: str) -> dict: # Prepare references to various files frontend if is_server_live: return { 'bundle_js': bundle_path, } else: _manifest = {} build_dir = os.path.join(app_dir, 'build') # Add the CRA static directory to STATICFILES_DIRS so collectstatic can grab files in there static_dir = os.path.join(build_dir, 'static') settings.STATICFILES_DIRS += [static_dir] # CRA generates a JSON file that maps typical filenames to their hashed filenames manifest_path = os.path.join(build_dir, _asset_filename) # Try to load the JSON manifest from the React build directory first try: with open(manifest_path) as data_file: logger.info('found manifest in React build files') data = json.load(data_file) except Exception as e: # If that doesn't work, try to load it from the Django project's static files directory try: static_manifest_path = os.path.join(settings.STATIC_ROOT, _asset_filename) with open(static_manifest_path) as data_file: logger.info('found manifest in static files') data = json.load(data_file) except Exception as e: logger.error('can\'t load static asset manifest: {}'.format(e)) return {} # Generate relative paths to our bundled assets for filename, path in data.items(): asset_key = filename.replace('.', '_') asset_key = asset_key.replace('/', '_') _manifest[asset_key] = os.path.relpath(path, 'static/') return _manifest
def patch_staticfiles(): settings.STATICFILES_DIRS += ( os.path.join(BASE_DIR, 'django_executor', 'staticfiles'), ) # Add DJANGO_EXECUTOR_CONFIG to root settings
def staticfiles(request, file): """ Simple view for serving static files directly from STATICFILES_DIRS. Does not allow subdirectories. Does do If-Modified-Since, though. Based on `django.views.static.serve`. """ if '/..\\' in file: raise Http404 if posixpath.normpath(file) != file: raise Http404 for static_file_dir in settings.STATICFILES_DIRS: fullpath = os.path.abspath(os.path.join(static_file_dir, file)) if not fullpath.startswith(static_file_dir): raise Http404 try: st = os.stat(fullpath) except FileNotFoundError: continue break else: raise Http404 if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'), st.st_mtime, st.st_size): return HttpResponseNotModified() content_type, encoding = mimetypes.guess_type(fullpath) content_type = content_type or 'application/octet-stream' response = FileResponse(open(fullpath, 'rb'), content_type=content_type) response['Last-Modified'] = http_date(st.st_mtime) if stat.S_ISREG(st.st_mode): response['Content-Length'] = st.st_size if encoding: response['Content-Encoding'] = encoding return response
def find(self, path, all=False): """ Looks for files in the extra locations as defined in ``STATICFILES_DIRS``. """ matches = [] for prefix, root in self.locations: matched_path = self.find_location(root, path, prefix) if matched_path: if not all: return matched_path matches.append(matched_path) return matches