ListView django 内置的一个View,用于方便展示列表数据,可以很快实现分页展示。
1、关键变量。
- context_object_name————在模板中的变量名。{{name}}
- template_name————-模板一般是一个html文件名
- paginate_by————如果做分页这个参数说明每页有几个item项
- model——————对应的模型(Model)
- http_method_names———-请求类型 可以是get或者post
2、方法。
- get_queryset——–或者需要展示的数据并且返回(必须要有返回),
- get_context_data——–传递额外的数据到模板(html)。
def get_queryset(self):
post_lists = []
给post_lists 赋值.........
return post_lists
def get_context_data(self, **kwargs):
context = super(ServerListIndex, self).get_context_data(**kwargs)
context['message'] = message.object.filter()
return context
context对象又多了一个key为message的数据