From f386e08ef1457338cfbadfbcc3aacb431dd3eae9 Mon Sep 17 00:00:00 2001 From: erjemin Date: Mon, 10 Aug 2026 18:05:19 +0300 Subject: [PATCH] =?UTF-8?q?mod:=20=D0=BF=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20django-debug-toolbar=20=D0=B2?= =?UTF-8?q?=20=D1=80=D0=BE=D1=83=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hypn0/hypn0/urls.py | 46 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/hypn0/hypn0/urls.py b/hypn0/hypn0/urls.py index 1a13ad9..726bc7f 100644 --- a/hypn0/hypn0/urls.py +++ b/hypn0/hypn0/urls.py @@ -15,8 +15,52 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import include, path +from django.conf.urls.static import static +from hypn0 import settings urlpatterns = [ path('admin/', admin.site.urls), ] + +if settings.DEBUG: + import mimetypes + import debug_toolbar + from django.views.static import serve + + def _serve_public_root_file(request, path): + """Отдаёт файлы из корня `public` в dev-режиме в utf-8.""" + response = serve(request, path, document_root=settings.PUBLIC_DIR) + content_type, _ = mimetypes.guess_type(path) + if content_type: + if content_type.startswith('text/'): + response['Content-Type'] = f'{content_type}; charset=utf-8' + else: + response['Content-Type'] = content_type + elif path.endswith('.txt'): + response['Content-Type'] = 'text/plain; charset=utf-8' + elif path.endswith('.html'): + response['Content-Type'] = 'text/html; charset=utf-8' + return response + + def _iter_public_root_files(): + """Находит все обычные файлы в корне `public`, кроме служебных артефактов.""" + for file_path in sorted(settings.PUBLIC_DIR.iterdir()): + if not file_path.is_file(): + continue + if file_path.name.startswith('.'): + continue + if file_path.name == 'README.md': + continue + yield file_path.name + + PUBLIC_ROOT_URLPATTERNS = [ + path(filename, _serve_public_root_file, {'path': filename}) + for filename in _iter_public_root_files() + ] + + urlpatterns = [path('__debug__/', include(debug_toolbar.urls)), ] + urlpatterns + # urlpatterns = [path('tmp/', views.tmp, name='web_tmp')] + urlpatterns + urlpatterns = [*PUBLIC_ROOT_URLPATTERNS, *urlpatterns] + urlpatterns += static(settings.STATIC_URL, document_root=settings.PUBLIC_DIR.joinpath('static')) + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)