• Blog_Django(六):blog分页列出文章


    在blog中,列出所有文章,需要分如下几步:

    第一步:视图函数

    from django.core.paginator import Paginator, InvalidPage, EmptyPage, PageNotAnInteger
    
    
    
    def index(request):
        try:
            f = open("a.txt", "r")
        except Exception as e:
            logger.error(e)
    
        category_list = Category.objects.all()
    
        # 文章分页
        all_article = Article.objects.all()
        paginator = Paginator(all_article, per_page=2)
        page_index = int(request.GET.get('page', 1))
        try:
            article_list = paginator.page(page_index)
        except (InvalidPage, EmptyPage, PageNotAnInteger) as e:
            article_list = paginator.page(1)
        
        return render(request, "index.html", {"category_list":category_list, "article_list":article_list})
    views.py

    django自带分页器Paginator,需要两个参数:数据列表,每页大小。

    第二步:页面

    {% extends "main/base.html" %}
    {% load staticfiles %}
    {% block content %}
        <div class="topnews">
            <h2>最新文章</h2>
            {% for article in article_list %}
                <div class="blogs">
                    <ul>
                        <h3><a href="/">{{ article.title }}</a></h3>
                        <p>{{ article.description }}</p>
                        <p class="autor">
                            <span class="lm f_l">
                                <a href="/">
                                    {% for tag in article.tag.all %}{{ tag.name }}&nbsp;{% endfor %}
                                </a>
                            </span>
                            <span class="dtime f_l">{{ article.date_publish|date:'Y-m-d' }}</span>
                            <span class="viewnum f_r">浏览(<a href="/">{{ article.click_count }}</a></span>
                            <span class="pingl f_r">评论(<a href="/">{{ article.comment_set.count }}</a></span></p>
                    </ul>
                </div>
            {% endfor %}
        </div>
        {#分页#}
        <div id="pagination">
            <ul id="pagination-flickr">
                {% if article_list.has_previous %}
                    <li class="previous"><a href="?page={{ article_list.previous_page_number }}">&laquo;上一页</a></li>
                {% else %}
                    <li class="previous-off">&laquo;上一页</li>
                {% endif %}
                <li class="active">{{ article_list.number }}/{{ article_list.paginator.num_pages }}</li>
                {% if article_list.has_next %}
                    <li class="next"><a href="?page={{ article_list.next_page_number }}">下一页 &raquo;</a></li>
                {% else %}
                    <li class="next-off">下一页 &raquo;</li>
                {% endif %}
            </ul>
        </div>
    {% endblock %}
    index.html

    重点使用了django的分页器。

  • 相关阅读:
    Spring中使用Log4j记录日志
    Spring MVC异常处理实例
    Spring MVC静态资源实例
    Spring MVC页面重定向实例
    Spring MVC表单实例
    Eclipse4.6安装Tomcat插件时报错:Unable to read repository at http://tomcatplugin.sf.net/update/content.xml. Received fatal alert: handshake_failure
    Graphviz--图形绘制工具
    使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境
    MySQL在并发场景下的问题及解决思路
    MIT KIT OpenID Connect Demo Client
  • 原文地址:https://www.cnblogs.com/yangshl/p/6505567.html
Copyright © 2020-2023  润新知