Skip to content

Commit

Permalink
为评论增加分页功能
Browse files Browse the repository at this point in the history
  • Loading branch information
liangliangyy committed Jul 13, 2022
1 parent 1ad5d34 commit a448e8b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
2 changes: 1 addition & 1 deletion blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def comment_list(self):
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
Expand Down
18 changes: 16 additions & 2 deletions blog/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -119,9 +119,23 @@ def get_context_data(self, **kwargs):
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

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
50 changes: 34 additions & 16 deletions templates/comments/tags/comment_list.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
<section id="comments" class="themeform">
{% load blog_tags %}
{% load comments_tags %}
{% load cache %}
<dev>
<section id="comments" class="themeform">
{% load blog_tags %}
{% load comments_tags %}
{% load cache %}

<ul class="comment-tabs group">
<li class="active"><a href="#commentlist-container"><i
class="fa fa-comments-o"></i>评论<span>{{ comment_count }}</span></a></li>
<ul class="comment-tabs group">
<li class="active"><a href="#commentlist-container"><i
class="fa fa-comments-o"></i>评论<span>{{ comment_count }}</span></a></li>

</ul>
{% if article_comments %}
{% cache 36000 article_comments article.id %}
</ul>
{% if article_comments %}
<div id="commentlist-container" class="comment-tab" style="display: block;">
<ol class="commentlist">
{% 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 %}

</ol><!--/.commentlist-->

<div class="navigation">
<nav class="nav-single">
{% if comment_prev_page_url %}
<div class="nav-previous">
<span><a href="{{ comment_prev_page_url }}" rel="prev"><span
class="meta-nav"></span> 上一页</a></span>
</div>
{% endif %}
{% if comment_next_page_url %}
<div class="nav-next">
<span><a href="{{ comment_next_page_url }}" rel="next">下一页 <span
class="meta-nav"></span></a></span>
</div>
{% endif %}
</nav>
</div>
<br/>
</div>
{% endcache %}
{% endif %}
</section>
{% endif %}
</section>

</dev>

0 comments on commit a448e8b

Please sign in to comment.