从数组创建多个页面 [英] Create several pages from array

查看:57
本文介绍了从数组创建多个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发有关Civil State的 Django项目,并且根据我的 HTML模板有一个问题.

I'm developing my Django Project about Civil State and I have a question according to my HTML template.

我正在显示 BirthCertificate表中的对象列表以及 HTML数组,如您在脚本中看到的那样:

I'm displaying a list of objects from my BirthCertificate table with an HTML Array as you can see in my script :

{% extends 'Base_Accueil.html' %}

{% load staticfiles %}

{% block content %}

{% load bootstrap %}

        <style>
        table, th, td {
        border: 1px solid black;
        padding: 10px;
        border-collapse: collapse;
        text-align: center;
        }

        th {background-color: #0083A2;}

        tr:hover td{background-color:lightslategray;}

        </style>

        <!-- Title page -->

        <h2 align="center"> <font color="#0083A2"> Consultation des tables annuelles et décennales </font></align></h2>

        <!-- Body page -->

        <br></br>
        <h4> <b><font color="#0083A2"> <span class="glyphicon glyphicon-user"></span> Liste des actes de naissance </b></font></h4>

        <form method="GET" action="">
            <input type="text"  name="q1" placeholder="Entrer une année" value="{{ request.GET.q1 }}"> &nbsp;
            <input class="button" type="submit" value="Rechercher">
        </form>

        <br></br>

        <table style="width:50%">
            <tbody>
                <tr>
                    <th>ID</th>
                    <th>Nom</th>
                    <th>Prénom</th>
                    <th>Date de Naissance</th>
                    <th>Ville de Naissance</th>
                    <th>Pays de Naissance</th>
                    <th>Date création de l'acte</th>
                </tr>
                {% for item in query_naissance_list %}
                <tr>
                    <td> {{item.id}} </td>
                    <td> {{item.lastname}} </td>
                    <td> {{item.firstname}} </td>
                    <td> {{item.birthday}} </td>
                    <td> {{item.birthcity}} </td>
                    <td> {{item.birthcountry}} </td>
                    <td>{{item.created}}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

    </div>

{% endblock content %}

我获得此网站页面:

我的问题是:如何从数组中添加一些页面,例如以十乘十显示列表?我必须使用哪种语言: Python HTML Javascript (不是更好的方法吗?),...?

My question is : How I can add some pages from my array in order to display the list ten by ten for example ? Which langage I have to use : Python, HTML, Javascript (better way isn't it ?), ... ?

这是我第一次做这种事情,但我在StackOverFlow上找不到其他线索.

It's the first time I'm making this kind of things and I don't find on StackOverFlow or somewhere else clues to do that.

非常感谢您!

推荐答案

可能您正在寻找

Probably you are looking for pagination. You can add pages to the list view with Paginator.

在您的视图中执行以下操作:

Do something like this in your view:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
paginator = Paginator(query_naissance_list, 10)
page = request.GET.get('page')
try:
    query_naissance_list = paginator.page(page)
except PageNotAnInteger:
    query_naissance_list = paginator.page(1)
except EmptyPage:
    query_naissance_list = paginator.page(paginator.num_pages)

在模板中添加导航:

    {% if query_naissance_list.has_previous %}
        <a href="?page={{ query_naissance_list.previous_page_number }}">previous</a>
    {% endif %}

    <span class="current">
        Page {{ query_naissance_list.number }} of {{ query_naissance_list.paginator.num_pages }}.
    </span>

    {% if query_naissance_list.has_next %}
        <a href="?page={{ query_naissance_list.next_page_number }}">next</a>
    {% endif %}

这篇关于从数组创建多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆