diff --git a/blog/models.py b/blog/models.py index 60a89c0..133ca57 100644 --- a/blog/models.py +++ b/blog/models.py @@ -145,7 +145,7 @@ class Article(BaseModel): logger.info('get article comments:{id}'.format(id=self.id)) return value else: - comments = self.comment_set.filter(is_enable=True) + comments = self.comment_set.filter(is_enable=True).order_by('-id') cache.set(cache_key, comments, 60 * 100) logger.info('set article comments:{id}'.format(id=self.id)) return comments diff --git a/blog/views.py b/blog/views.py index 007154d..8b79239 100644 --- a/blog/views.py +++ b/blog/views.py @@ -1,10 +1,10 @@ import datetime import logging -# Create your views here. import os import uuid from django.conf import settings +from django.core.paginator import Paginator from django.http import HttpResponse, HttpResponseForbidden from django.shortcuts import get_object_or_404 from django.shortcuts import render @@ -119,9 +119,23 @@ class ArticleDetailView(DetailView): comment_form = CommentForm() article_comments = self.object.comment_list() - + parent_comments = article_comments.filter(parent_comment=None) + + paginator = Paginator(parent_comments, 5) + page = self.request.GET.get('comment_page', 1) + p_comments = paginator.page(page) + next_page = p_comments.next_page_number() if p_comments.has_next() else None + prev_page = p_comments.previous_page_number() if p_comments.has_previous() else None + + if next_page: + kwargs[ + 'comment_next_page_url'] = self.object.get_absolute_url() + f'?comment_page={next_page}#commentlist-container' + if prev_page: + kwargs[ + 'comment_prev_page_url'] = self.object.get_absolute_url() + f'?comment_page={prev_page}#commentlist-container' kwargs['form'] = comment_form kwargs['article_comments'] = article_comments + kwargs['p_comments'] = p_comments kwargs['comment_count'] = len( article_comments) if article_comments else 0 diff --git a/docker-compose.yml b/docker-compose.yml index b1e130b..47a0b37 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,6 +32,7 @@ services: - "8000:8000" volumes: - ./collectedstatic:/code/djangoblog/collectedstatic + - ./logs:/code/djangoblog/logs environment: - DJANGO_MYSQL_DATABASE=djangoblog - DJANGO_MYSQL_USER=root diff --git a/requirements.txt b/requirements.txt index 0c32cd2..dc13859 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,7 @@ gevent==21.12.0 jieba==0.42.1 jsonpickle==2.1.0 Markdown==3.3.7 -mysqlclient==2.1.0 +mysqlclient==2.1.1 Pillow==9.1.1 Pygments==2.12.0 python-logstash==0.4.6 @@ -24,4 +24,4 @@ urllib3==1.26.9 WeRoBot==1.13.1 Whoosh==2.7.4 user-agents==2.2.0 -redis==4.3.3 +redis==4.3.4 diff --git a/templates/comments/tags/comment_list.html b/templates/comments/tags/comment_list.html index 79d36c0..4092161 100644 --- a/templates/comments/tags/comment_list.html +++ b/templates/comments/tags/comment_list.html @@ -1,27 +1,45 @@ -
- {% load blog_tags %} - {% load comments_tags %} - {% load cache %} + +
+ {% load blog_tags %} + {% load comments_tags %} + {% load cache %} - + {% if article_comments %}
    - {% query article_comments parent_comment=None as parent_comments %} - {% for comment_item in parent_comments %} + {# {% query article_comments parent_comment=None as parent_comments %}#} + {% for comment_item in p_comments %} + {% with 0 as depth %} {% include "comments/tags/comment_item_tree.html" %} {% endwith %} {% endfor %}
- + +
- {% endcache %} - {% endif %} -
+ {% endif %} +
+ + \ No newline at end of file