Skip to content

Commit a448e8b

Browse files
committed
为评论增加分页功能
1 parent 1ad5d34 commit a448e8b

File tree

5 files changed

+54
-21
lines changed

5 files changed

+54
-21
lines changed

blog/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def comment_list(self):
145145
logger.info('get article comments:{id}'.format(id=self.id))
146146
return value
147147
else:
148-
comments = self.comment_set.filter(is_enable=True)
148+
comments = self.comment_set.filter(is_enable=True).order_by('-id')
149149
cache.set(cache_key, comments, 60 * 100)
150150
logger.info('set article comments:{id}'.format(id=self.id))
151151
return comments

blog/views.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import datetime
22
import logging
3-
# Create your views here.
43
import os
54
import uuid
65

76
from django.conf import settings
7+
from django.core.paginator import Paginator
88
from django.http import HttpResponse, HttpResponseForbidden
99
from django.shortcuts import get_object_or_404
1010
from django.shortcuts import render
@@ -119,9 +119,23 @@ def get_context_data(self, **kwargs):
119119
comment_form = CommentForm()
120120

121121
article_comments = self.object.comment_list()
122-
122+
parent_comments = article_comments.filter(parent_comment=None)
123+
124+
paginator = Paginator(parent_comments, 5)
125+
page = self.request.GET.get('comment_page', 1)
126+
p_comments = paginator.page(page)
127+
next_page = p_comments.next_page_number() if p_comments.has_next() else None
128+
prev_page = p_comments.previous_page_number() if p_comments.has_previous() else None
129+
130+
if next_page:
131+
kwargs[
132+
'comment_next_page_url'] = self.object.get_absolute_url() + f'?comment_page={next_page}#commentlist-container'
133+
if prev_page:
134+
kwargs[
135+
'comment_prev_page_url'] = self.object.get_absolute_url() + f'?comment_page={prev_page}#commentlist-container'
123136
kwargs['form'] = comment_form
124137
kwargs['article_comments'] = article_comments
138+
kwargs['p_comments'] = p_comments
125139
kwargs['comment_count'] = len(
126140
article_comments) if article_comments else 0
127141

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ services:
3232
- "8000:8000"
3333
volumes:
3434
- ./collectedstatic:/code/djangoblog/collectedstatic
35+
- ./logs:/code/djangoblog/logs
3536
environment:
3637
- DJANGO_MYSQL_DATABASE=djangoblog
3738
- DJANGO_MYSQL_USER=root

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ gevent==21.12.0
1212
jieba==0.42.1
1313
jsonpickle==2.1.0
1414
Markdown==3.3.7
15-
mysqlclient==2.1.0
15+
mysqlclient==2.1.1
1616
Pillow==9.1.1
1717
Pygments==2.12.0
1818
python-logstash==0.4.6
@@ -24,4 +24,4 @@ urllib3==1.26.9
2424
WeRoBot==1.13.1
2525
Whoosh==2.7.4
2626
user-agents==2.2.0
27-
redis==4.3.3
27+
redis==4.3.4
Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
1-
<section id="comments" class="themeform">
2-
{% load blog_tags %}
3-
{% load comments_tags %}
4-
{% load cache %}
1+
<dev>
2+
<section id="comments" class="themeform">
3+
{% load blog_tags %}
4+
{% load comments_tags %}
5+
{% load cache %}
56

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

10-
</ul>
11-
{% if article_comments %}
12-
{% cache 36000 article_comments article.id %}
11+
</ul>
12+
{% if article_comments %}
1313
<div id="commentlist-container" class="comment-tab" style="display: block;">
1414
<ol class="commentlist">
15-
{% query article_comments parent_comment=None as parent_comments %}
16-
{% for comment_item in parent_comments %}
15+
{# {% query article_comments parent_comment=None as parent_comments %}#}
16+
{% for comment_item in p_comments %}
17+
1718
{% with 0 as depth %}
1819
{% include "comments/tags/comment_item_tree.html" %}
1920
{% endwith %}
2021
{% endfor %}
2122

2223
</ol><!--/.commentlist-->
23-
24+
<div class="navigation">
25+
<nav class="nav-single">
26+
{% if comment_prev_page_url %}
27+
<div class="nav-previous">
28+
<span><a href="{{ comment_prev_page_url }}" rel="prev"><span
29+
class="meta-nav"></span> 上一页</a></span>
30+
</div>
31+
{% endif %}
32+
{% if comment_next_page_url %}
33+
<div class="nav-next">
34+
<span><a href="{{ comment_next_page_url }}" rel="next">下一页 <span
35+
class="meta-nav"></span></a></span>
36+
</div>
37+
{% endif %}
38+
</nav>
39+
</div>
40+
<br/>
2441
</div>
25-
{% endcache %}
26-
{% endif %}
27-
</section>
42+
{% endif %}
43+
</section>
44+
45+
</dev>

0 commit comments

Comments
 (0)