前戏

分页是我们经常遇到的,只要有表单,数据量大的时候,都要用到分页,所以说分页是web开发里必不可少的知识点。

分页第一版

使用列表生成式来制造一些数据

user_data = [{'name':'zouzou{}'.format(i),'pwd':'abc{}'.format(i)} for i in range(1,303)]

在写个视图函数,把数据传给html文件进行渲染

def user_list(request):
    

    return render(request, 'user_list.html', {'data': user_data})

写一个HTML文件用于展示数据

{% extends 'layout.html' %}

{% block content %}
    <table class="table table-bordered">
        <thead>
            <th>序号</th>
            <th>账号</th>
            <th>密码</th>
        </thead>
        <tbody>
        {% for user in data %}
            <tr>
                <td>{{ forloop.counter }}</td>
                <td>{{ user.name }}</td>
                <td>{{ user.pwd }}</td>
            </tr>
        {% endfor %}

        </tbody>
    </table>



{% endblock content %}
user_list.html

相关文章: