首页 > 代码库 > django generic view - ListView

django generic view - ListView

 ListView (带分页)

1.带分页效果的基础模板

1) view

from django.views.generic.list import ListView

from employ.models import Company


class CompanyListView(ListView):
    model = Company
    context_object_name = "companies"
    template_name = company/company_list.html
    paginate_by = 20

2) url

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r^$, views.CompanyListView.as_view(), name=company-list),

]

3) template

<table class="table table-striped">
    <thead>
        <tr>
            <th>名称</th>
            <th>规模</th>
        </tr>
    </thead>
    <tbody>
        {% for company in companies %}
        <tr>
            <td>{{ company.name }}</td>
            <td>{{ company.scale }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

{% if is_paginated %}
<div class="pagination center-block">
    <span>共{{ page_obj.paginator.num_pages }}页</span>
    <span id="paging" class="step-links" style="display: block;text-align: center">
        {% if page_obj.has_previous %}
        <span><a href=http://www.mamicode.com/"?page={{ page_obj.previous_page_number }}">上一页</a></span>
        {% endif %}
        <span class="active">{{ page_obj.number }}</span>
        {% if page_obj.has_next %}
        <span><a href=http://www.mamicode.com/"?page={{ page_obj.next_page_number }}">下一页</a></span>
        {% endif %}
    </span>
</div>
{% endif %}

 

2.model < queryset < get_queryset()

这时候,页面上显示是model里的所有对象。如果我们希望这个object list排序后呈现呢?或者想要筛选部分呈现?这些又在哪定义呢?

很简单,这时候只要定义queryset或者重写get_queryset()方法就能搞定。

注意:model、queryset、get_queryset()至少要定义一个。而当三个都定义的时候,get_queryset()的效果强于queryset,而queryset的效果又强于model。

2.1 定义queryset

from django.views.generic.list import ListView

from employ.models import Company


class CompanyListView(ListView):
    context_object_name = "companies"
    template_name = company/company_list.html
    paginate_by = 20
    queryset = Company.objects.all().order_by(-scale)   

 

2.2 重写get_queryset()

from django.views.generic.list import ListView

from employ.models import Company


class CompanyListView(ListView):
    context_object_name = "companies"
    template_name = company/company_list.html
    paginate_by = 20
def get_queryset(self): companies = Company.objects.all().order_by(-scale) return companies

 

3.向template传递额外信息

以上,我们只向template中传递了关于某个Model里的所有对象。那么,我们有什么方法传递更多信息呢?

方法一:重写get_context_data()。如我想传递当前时间显示在template中:

view:

from django.views.generic.list import ListView

from employ.models import Company

from datetime import datetime


class CompanyListView(ListView):
    context_object_name = "companies"
    template_name = company/company_list.html
    paginate_by = 20
    queryset = Company.objects.all().order_by(-open_positions)

    def get_context_data(self, **kwargs):
        context = super(CompanyListView, self).get_context_data(**kwargs)
        context[now] = datetime.now()
        return context

template:

{{ now }}

 

方法二:在view中定义some_method()方法返回数据,在模版中使用{{ view.some_method }}。

注意:some_method(self)方法只能有一个self参数。

view:

from django.views.generic.list import ListView

from employ.models import Company

from datetime import datetime


class CompanyListView(ListView):
    context_object_name = "companies"
    template_name = company/company_list.html
    paginate_by = 20
    queryset = Company.objects.all().order_by(-open_positions)

    def now(self):
        return datetime.now()

template:

{{ view.now }}

 

django generic view - ListView