Django之分页显示文章

1.项目:http://www.cnblogs.com/jasonhaven/p/7493422.html

2.任务描述:页面分页显示文章

3.源代码

后台:

from django.core.paginator import Paginator, InvalidPage, EmptyPage, PageNotAnInteger
from models import *
def getPage(request):
    article_list = Article.objects.all()
    paginator = Paginator(article_list, 2)
    try:
        page = int(request.GET.get('page', 1))
        article_list = paginator.page(page)
    except (EmptyPage, InvalidPage, PageNotAnInteger):
        article_list = paginator.page(1)
    return article_list

前台:

{% block left_content %}
    {% include 'ad.html' %}
    

最新文章

{% for article in article_list %}
{% endfor %}
{% include 'pagination.html' %} {% endblock %}

pageination.html

4.运行结果

Django之分页显示文章_第1张图片

 

转载于:https://www.cnblogs.com/jasonhaven/p/7520555.html

你可能感兴趣的:(Django之分页显示文章)