Как сервер статических файлов в папке без папки 'static'? Django
У меня есть мой index.html и статические файлы в такой структуре:
- шаблоны:
- папка:
- index.html
- css
- js
- images
- another_folder:
- index.html
- css
- js
- images
- папка:
Я хочу, чтобы staticfiles finder искал static в шаблонах по доменному имени в запросе. Пожалуйста, без папки 'static' и тега {% load static %}. Спасибо.
Оки, я нашел решение. Вот url для статических данных: re_path(r'^(?P<path>.*)$', static, {'document_root': 'templates/lands/'}),
Вот мой вид:
def static(request, path, document_root=None, show_indexes=False):
domain = request.get_host()
path = domain + '/' + posixpath.normpath(path).lstrip('/')
fullpath = safe_join(document_root, path)
statobj = os.stat(fullpath)
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(statobj.st_mtime)
if stat.S_ISREG(statobj.st_mode):
response["Content-Length"] = statobj.st_size
if encoding:
response["Content-Encoding"] = encoding
return response