handle media and static files for uploads

This commit is contained in:
faraphel-gima 2023-11-28 12:09:21 +01:00
parent 75c931eb74
commit 2d5d70972f
3 changed files with 21 additions and 3 deletions

View file

@ -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")

View file

@ -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

View file

@ -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<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}),
]