From 2d5d70972f241d67d8d7ddad9e07073ff832e334 Mon Sep 17 00:00:00 2001 From: faraphel-gima Date: Tue, 28 Nov 2023 12:09:21 +0100 Subject: [PATCH] handle media and static files for uploads --- Palto/Palto/models.py | 3 ++- Palto/settings.py | 8 +++++++- Palto/urls.py | 13 ++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Palto/Palto/models.py b/Palto/Palto/models.py index 501b9e5..4344335 100644 --- a/Palto/Palto/models.py +++ b/Palto/Palto/models.py @@ -1,3 +1,4 @@ +import os import uuid from datetime import datetime, timedelta @@ -136,6 +137,6 @@ class AbsenceAttachment(models.Model): """ id: uuid.UUID = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False, max_length=36) - content = models.FileField() + content = models.FileField(upload_to="absence/attachment/") absence = models.ForeignKey(to=Absence, on_delete=models.CASCADE, related_name="attachments") diff --git a/Palto/settings.py b/Palto/settings.py index 007c580..7d3d7cb 100644 --- a/Palto/settings.py +++ b/Palto/settings.py @@ -131,7 +131,13 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.2/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = "static/" +STATIC_ROOT = BASE_DIR / "static-collected/" + +# Media files + +MEDIA_URL = "media/" +MEDIA_ROOT = BASE_DIR / "media/" # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/Palto/urls.py b/Palto/urls.py index ac71b6d..17dadbc 100644 --- a/Palto/urls.py +++ b/Palto/urls.py @@ -15,7 +15,11 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path, include +from django.urls import path, include, re_path +from django.views.static import serve + +from Palto import settings + urlpatterns = [ # Application @@ -28,3 +32,10 @@ urlpatterns = [ path('admin/', admin.site.urls), # Admin page path("__debug__/", include("debug_toolbar.urls")), # Debug toolbar ] + + +if settings.DEBUG: + urlpatterns += [ + re_path(r'^media/(?P.*)$', serve, {'document_root': settings.MEDIA_ROOT}), + re_path(r'^static/(?P.*)$', serve, {'document_root': settings.STATIC_ROOT}), + ]