zoukankan      html  css  js  c++  java
  • Django-Mysql数据库查询并将查询结果分页显示

    
    
    # views.py
    def search(request):
        if request.method == "GET":  # 如果是翻页将会进入到get方式中
            name = request.session.get('name')
             # 进行数据库查询
            content = Book.objects.filter(name=name)#这里返回的是多条数据
            if content.exists():
               paginator = Paginator(content, 5)   # 每页显示5条
               try:
                     num = request.GET.get('index', 1)  # 页面连接,用于翻页
                     number = paginator.page(num)
               except PageNotAnInteger:
                     number = paginator.page(1)
               except EmptyPage:
                     number = paginator.page(paginator.num_pages)
               return render(request,'first.html','paginator':'paginator')
            return render(request,'first.html')
    
        else:
            name = request.POST.get('name')
            request.session['name'] = name   # POST 方式中用于接收前台数据并存入session
            # 进行数据库查询
            content = Book.objects.filter(name=name)#这里返回的是多条数据
            if content.exists():
                  paginator = Paginator(content, 5)   # 每页显示5条
                  try:
                     num = request.GET.get('index', 1)  # 页面连接,用于翻页
                     number = paginator.page(num)
                  except PageNotAnInteger:
                     number = paginator.page(1)
                  except EmptyPage:
                     number = paginator.page(paginator.num_pages)
                  return render(request,'first.html','paginator':'paginator')
             return render(request,'first.html')
            
    
    
    
    <!--first.html-->
    {% for item in page.object_list%}
    {{item.name}}
    {%endfor%}
    
    <!--分页部分-->
     <div>
                            <nav aria-label="Page navigation">
                                <ul class="pagination">
                                    {% if page.has_previous %}
                                        <li>
                                            <a href="?index={{ page.previous_page_number }}" aria-label="Previous">
                                                <span aria-hidden="true">&laquo;</span>
                                            </a>
                                        </li>
                                    {% else %}
                                        <li class="disabled">
                                            <a href="#" aria-label="Previous">
                                                <span aria-hidden="true">&laquo;</span>
                                            </a>
                                        </li>
                                    {% endif %}
    
                                    {% for page_number in paginator.page_range %}
                                        {% if page_number == page.number %}
                                            <li class="active"><a href="#">{{ page_number }}</a></li>
                                        {% else %}
                                            <li><a href="?index={{ page_number }}">{{ page_number }}</a></li>
                                        {% endif %}
    
                                    {% endfor %}
                                    {% if page.has_next %}
                                        <li>
                                            <a href="?index={{ page.next_page_number }}" aria-label="Next">
                                                <span aria-hidden="true">&raquo;</span>
                                            </a>
                                        </li>
                                    {% else %}
                                        <li class="disabled">
                                            <a href="#" aria-label="Next">
                                                <span aria-hidden="true">&raquo;</span>
                                            </a>
                                        </li>
                                    {% endif %}
    
                                </ul>
                            </nav>
    
                        </div>
    
  • 相关阅读:
    HDU 2192 MagicBuilding
    HDU 2148 Score
    HDU 1847 Good Luck in CET4 Everybody!
    往CImageList中加图标列表
    LoadIcon
    OnInitialUpdate 详解
    设备坐标(DP)、客户坐标(Client)、逻辑坐标(LP)
    Web及网络基础学习(一)
    Qt 下QMessageBox下中文乱码问题
    vs2005菜单:选项项目和解决方案
  • 原文地址:https://www.cnblogs.com/lelezuimei/p/13600892.html
Copyright © 2011-2022 走看看