From 830120a9294a31eed62a2f9ed7764944af91a43e Mon Sep 17 00:00:00 2001 From: reverend Date: Thu, 13 Jan 2022 17:23:57 +0100 Subject: [PATCH] #59 Pictures are in the wrong directory --- .gitignore | 5 ++++- django_lostplaces/django_lostplaces/settings.py | 11 ++++++----- django_lostplaces/django_lostplaces/urls.py | 6 +++++- django_lostplaces/lostplaces/models/place.py | 3 ++- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 1583b97..0c6996b 100644 --- a/.gitignore +++ b/.gitignore @@ -93,4 +93,7 @@ venv.bak/ .pypirc # Django Migrations for Development branches -django_lostplaces/lostplaces/migrations/* \ No newline at end of file +django_lostplaces/lostplaces/migrations/* + +# Django Static files +django_lostplaces/static/* \ No newline at end of file diff --git a/django_lostplaces/django_lostplaces/settings.py b/django_lostplaces/django_lostplaces/settings.py index 44c6948..9d41d7b 100644 --- a/django_lostplaces/django_lostplaces/settings.py +++ b/django_lostplaces/django_lostplaces/settings.py @@ -32,7 +32,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = 'n$(bx8(^)*wz1ygn@-ekt7rl^1km*!_c+fwwjiua8m@-x_rpl0' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = False ALLOWED_HOSTS = ['localhost'] @@ -145,15 +145,16 @@ LANGUAGES = [ # https://docs.djangoproject.com/en/3.1/howto/static-files/ STATIC_URL = '/static/' -STATIC_ROOT = os.path.join(BASE_DIR, 'static_files') +STATIC_ROOT = os.path.join(BASE_DIR, 'static') # Upload directory MEDIA_URL = '/uploads/' -MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads') +MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads/') # Thumbnails -THUMBNAIL_MEDIA_ROOT = os.path.join(MEDIA_ROOT, 'thumbs/') -THUMBNAIL_MEDIA_URL = os.path.join(MEDIA_URL, 'thumbs/') +RELATIVE_THUMBNAIL_PATH = 'images/' +THUMBNAIL_MEDIA_ROOT = os.path.join(MEDIA_ROOT, RELATIVE_THUMBNAIL_PATH) +THUMBNAIL_MEDIA_URL = os.path.join(MEDIA_URL, RELATIVE_THUMBNAIL_PATH) THUMBNAIL_QUALITY = 75 # Templates to use for authentication diff --git a/django_lostplaces/django_lostplaces/urls.py b/django_lostplaces/django_lostplaces/urls.py index 922dcfb..f9165ed 100644 --- a/django_lostplaces/django_lostplaces/urls.py +++ b/django_lostplaces/django_lostplaces/urls.py @@ -19,7 +19,9 @@ Including another URLconf from django.contrib import admin from django.conf import settings +from django.views.static import serve from django.conf.urls.static import static +from django.conf.urls import url from django.urls import path, include from django.views.generic.base import TemplateView @@ -30,4 +32,6 @@ urlpatterns = [ path('signup/', SignUpView.as_view(), name='signup'), path('explorer/', include('django.contrib.auth.urls')), path('', include('lostplaces.urls')), -] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + url(r'^static/(?P.*)$', serve,{'document_root': settings.STATIC_ROOT}), + url(r'^uploads/(?P.*)$', serve,{'document_root': settings.MEDIA_ROOT}) +] diff --git a/django_lostplaces/lostplaces/models/place.py b/django_lostplaces/lostplaces/models/place.py index 9ec4c99..7bb5520 100644 --- a/django_lostplaces/lostplaces/models/place.py +++ b/django_lostplaces/lostplaces/models/place.py @@ -6,6 +6,7 @@ from django.urls import reverse from django.dispatch import receiver from django.db.models.signals import post_delete, pre_save from django.utils.translation import ugettext_lazy as _ +from django.conf import settings from lostplaces.models.abstract_models import Submittable, Taggable, Mapable, Expireable @@ -120,7 +121,7 @@ def generate_place_image_filename(instance, filename): Returns filename as: place_pk-placename{-number}.jpg """ - return 'places/' + str(instance.place.pk) + '-' + str(instance.place.name) + '.' + filename.split('.')[-1] + return settings.RELATIVE_THUMBNAIL_PATH + str(instance.place.pk) + '-' + str(instance.place.name) + '.' + filename.split('.')[-1] def generate_image_upload_path(instance, filename): return generate_place_image_filename(instance, filename)