From 02867a4b9d22bd6f2fed9306a8d57ca73ae06b03 Mon Sep 17 00:00:00 2001 From: andis Date: Fri, 12 Jun 2020 21:22:12 +0000 Subject: [PATCH 1/3] local template dictionary first --- fet2020/fet2020/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fet2020/fet2020/settings.py b/fet2020/fet2020/settings.py index ca8321cc..23fc6621 100644 --- a/fet2020/fet2020/settings.py +++ b/fet2020/fet2020/settings.py @@ -64,7 +64,7 @@ ROOT_URLCONF = 'fet2020.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [os.path.join(BASE_DIR, 'templates_design1')], + 'DIRS': [os.path.join(BASE_DIR, 'templates'),os.path.join(BASE_DIR, 'templates_design1')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ From 02c6875d6395f193ebb11152e6bb9fe9bfdc4f41 Mon Sep 17 00:00:00 2001 From: andis Date: Fri, 12 Jun 2020 21:23:00 +0000 Subject: [PATCH 2/3] alternative image from other post --- fet2020/posts/models.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/fet2020/posts/models.py b/fet2020/posts/models.py index 0ade12d0..8b4c7277 100644 --- a/fet2020/posts/models.py +++ b/fet2020/posts/models.py @@ -11,6 +11,9 @@ import re from rest_framework import serializers from django.db.models import Q +import logging +logger = logging.getLogger('posts') + class PostManager(models.Manager): def get_queryset(self): return super().get_queryset() @@ -80,11 +83,27 @@ class Post(models.Model): if self.image: return self.image.url else: - return "" + image=self.find_an_image() + if image: + return image.url + else: + return "" def key(self): return self.slug or self.id + def find_an_image(self): + "find an image via another post" + posts1=Post.objects.filter(slug__in=self.tags.names()).filter(image__isnull=False)[0:1].all() # Query all posts that have a slug that equals one of the tags + if len(posts1)>0: + return posts1.get().image + + posts2=self.tags.similar_objects() + for p in posts2: + if p.image is not None: + return p.image + return None + @property def url(self): return reverse('posts.show', kwargs={"id":self.slug}) From 38c63e0e74146e9cbd79b2e52036523851b50cf4 Mon Sep 17 00:00:00 2001 From: andis Date: Fri, 12 Jun 2020 21:23:15 +0000 Subject: [PATCH 3/3] related posts added to post show --- fet2020/posts/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fet2020/posts/views.py b/fet2020/posts/views.py index abe325ae..a22b0b9d 100644 --- a/fet2020/posts/views.py +++ b/fet2020/posts/views.py @@ -43,7 +43,8 @@ def show(request,id=None): p=Post.objects.get(id=int(id)) elif id != ""and not id is None: p=Post.objects.get(slug=(id)) - return render(request, 'posts/show.html', {"post":p,"next":get_next_dict().get(p.slug,None)}) + similar_posts = p.tags.similar_objects() + return render(request, 'posts/show.html', {"post":p,"next":get_next_dict().get(p.slug,None), "related_posts": similar_posts}) # Ajax function that is called to calculate a slug from the title def slug_calc(request):