From 40687ec3713b9d794ac9bf51a44f16bd17192697 Mon Sep 17 00:00:00 2001 From: Stefano Alberto Russo <stefano.russo@gmail.com> Date: Sat, 20 Feb 2021 13:59:13 +0100 Subject: [PATCH] Added serving static files if using the development server with DEBUG set to False. --- services/webapp/code/rosetta/urls.py | 32 ++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/services/webapp/code/rosetta/urls.py b/services/webapp/code/rosetta/urls.py index 2a91b45..9ca3b28 100644 --- a/services/webapp/code/rosetta/urls.py +++ b/services/webapp/code/rosetta/urls.py @@ -13,6 +13,11 @@ Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ + +import os +import django +from django.conf import settings +from django.conf.urls import include, url from django.contrib import admin from django.urls import include, path from django.conf.urls import url @@ -69,6 +74,29 @@ urlpatterns = [ ] -# This message here is quite useful when developing in autoreload mode -logger.info('Loaded URLs') + +#============================ +# Serve static if required +#============================ + +# Get admin files location +admin_files_path = '/'.join(django.__file__.split('/')[0:-1]) + '/contrib/admin/static/admin' + +if not settings.DEBUG: + + # Admin files + urlpatterns.append(url(r'^static/admin/(?P<path>.*)$', django.views.static.serve, {'document_root': admin_files_path} )) + + # Rosetta Core app files + document_root = 'rosetta/core_app/static' + + if os.path.isdir(document_root): + logger.info('Serving static files for app "core_app" from document root "{}"'.format(document_root)) + # Static + urlpatterns.append(url(r'^static/(?P<path>.*)$', django.views.static.serve, {'document_root': document_root} )) + else: + logger.warning('Not static files to serve?!') +else: + logger.info('Not serving static files at all as DEBUG=True (Django will do it automatically)') + -- GitLab