Pyhont Django
通用视图ListView
Django 提供了一组基于类的通用视图,包含了开发中大部分的通用功能,ListView就是解决列表的问题,简洁,快速
基本用法ListView
使用ListView 首先需要在文件的顶部加载ListView
from django.views.generic import ListView
在app的views.py文件中写一个属于自己的ListView
from django.views.generic import ListView
from app.models import RegRecord
class RegRecordListView(ListView):
model = RegRecord
template_name = "app/index.html"
paginate_by=20
简单介绍一下ListView的属性
model ListView 会根据model所指向的类型来创建列表,model=RegRecord,ListView会调用RegRecord.objects.all()
template_name 如果这个参数不设置,那么ViewList会默认使用regrecord_list.html,否则会根据template_name的值使用哪个页面展示列表。
paginate_by 如果这个参数不设置,ViewList返回的列表是没有翻页的,如果这个参数被设置了,例如上面的例子,每页显示20条数据
ViewList还会在上下文环境中提供几个用于翻页使用的参数paginator 提供页数num_pages,总数据数count和一个页数的迭代器page_range,以上都是paginator的方法,可以在页面中通过{
{paginator.num_pages}},{
{paginator.count}},{
{paginator.page_range}}使用
在app的urls.py里配置刚才编写的基于类的view。
from django.urls import path
from views import RegRecordListView
urlpatterns = [
path('',RegRecordListView.as_view(),name='index'),
]
<table>
<thead>
<th>111</th>
<th>111</th>
<th>111</th>
<th>111