Skip to content

Commit 28b7a20

Browse files
committed
完成分页功能
1 parent 1869993 commit 28b7a20

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

DjangoBlog/settings.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@
155155
# 侧边栏评论数目
156156
SIDEBAR_COMMENT_COUNT = 5
157157

158+
# 分页
159+
PAGINATE_BY = 10
160+
158161
# cache setting
159162
"""
160163
CACHES = {
@@ -167,8 +170,8 @@
167170

168171
OAHUTH = {
169172
'sina': {
170-
'appkey':'3161614143',
171-
'appsecret':'ee17c099317f872eeddb25204ea46721',
172-
'callbackurl':'http://blog.lylinux.org/oauth/weibo'
173+
'appkey': '3161614143',
174+
'appsecret': 'ee17c099317f872eeddb25204ea46721',
175+
'callbackurl': 'http://blog.lylinux.org/oauth/weibo'
173176
}
174177
}

blog/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# url(r'^$', cache_page(60 * 15)(views.IndexView.as_view()), name='index'),
2222
url(r'^$', views.IndexView.as_view(), name='index'),
2323
# url(r'^article/(?P<article_id>\d+)$', views.ArticleDetailView.as_view(), name='detail'),
24-
24+
url(r'^page/(?P<page>\d+)$', views.IndexView.as_view(), name='index_page'),
2525
url(r'(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<article_id>\d+)-(?P<slug>\S+).html$',
2626
views.ArticleDetailView.as_view(),
2727
name='detail'),

blog/views.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
from django.views.generic.edit import CreateView, FormView
88
from django.views.generic.dates import YearArchiveView, MonthArchiveView
99
from blog.models import Article, Category, Tag
10-
10+
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
1111
from django.core.exceptions import ObjectDoesNotExist
1212
from comments.forms import CommentForm
13-
13+
from django.conf import settings
1414
from django import forms
1515

16+
1617
class ArticleListView(ListView):
1718
# template_name属性用于指定使用哪个模板进行渲染
1819
template_name = 'blog/index.html'
@@ -22,10 +23,13 @@ class ArticleListView(ListView):
2223

2324
# 页面类型,分类目录或标签列表等
2425
page_type = ''
26+
paginate_by = settings.PAGINATE_BY
27+
page_kwarg = 'page'
2528

2629

2730
class IndexView(ArticleListView):
2831
def get_queryset(self):
32+
2933
article_list = Article.objects.filter(status='p')
3034

3135
# for article in article_list:
@@ -59,7 +63,6 @@ def get_article(id):
5963
comment_form = CommentForm()
6064

6165
if self.request.user.is_authenticated():
62-
6366
comment_form.fields.update({
6467
'email': forms.CharField(widget=forms.HiddenInput()),
6568
'name': forms.CharField(widget=forms.HiddenInput()),

favicon.ico

3.11 KB
Binary file not shown.

templates/blog/index.html

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@ <h1 class="archive-title">{{ page_type }}:<span>{{ tag_name }}</span></h1>
1414
{% for article in article_list %}
1515
{% load_article_detail article True %}
1616
{% endfor %}
17+
{% if is_paginated %}
18+
<nav id="nav-below" class="navigation" role="navigation">
19+
<h3 class="assistive-text">文章导航</h3>
20+
{% if page_obj.has_next %}
21+
<div class="nav-previous"><a
22+
href="{{ SITE_BASE_URL }}page/{{ page_obj.next_page_number }}"><span
23+
class="meta-nav">&larr;</span> 早期文章</a></div>
24+
{% endif %}
25+
{% if page_obj.has_previous %}
26+
<div class="nav-next"><a href="{{ SITE_BASE_URL }}page/{{ page_obj.previous_page_number }}">较新文章
27+
<span
28+
class="meta-nav"></span></a>
29+
</div>
30+
{% endif %}
31+
</nav><!-- .navigation -->
1732

18-
<nav id="nav-below" class="navigation" role="navigation">
19-
<h3 class="assistive-text">文章导航</h3>
20-
<div class="nav-previous"><a href="https://www.lylinux.org/page/2"><span
21-
class="meta-nav">&larr;</span> 早期文章</a></div>
22-
<div class="nav-next"></div>
23-
</nav><!-- .navigation -->
24-
25-
33+
{% endif %}
2634
</div><!-- #content -->
2735
</div><!-- #primary -->
2836
{% endblock %}

0 commit comments

Comments
 (0)