本文整理汇总了Python中django.views.generic.ListView方法的典型用法代码示例。如果您正苦于以下问题:Python generic.ListView方法的具体用法?Python generic.ListView怎么用?Python generic.ListView使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.views.generic
的用法示例。
在下文中一共展示了generic.ListView方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_list_class_view
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def get_list_class_view(self):
name = '{0}_{1}'.format(self.get_name(), 'ListView')
options_dict = {
'kind': 'list',
'model': self.model,
}
if hasattr(self, 'list_template_name') and self.list_template_name:
options_dict['template_name'] = self.list_template_name
parent_classes_list = [FallbackTemplateMixin]
parent_classes_list.extend(self.list_mixins)
parent_classes_list.append(self.list_view_class)
klazz = type(name, tuple(parent_classes_list), options_dict )
klazz.get_context_data = self.get_get_context_data(klazz)
return klazz
示例2: get_queryset
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def get_queryset(self, **kwargs):
queryset = Site.objects.filter(project_id=self.kwargs.get('pk'), is_survey=False, is_active=True)
if self.kwargs.get('region_id') == "0":
object_list = queryset.filter(region=None)
else:
object_list = queryset.filter(region_id=self.kwargs.get('region_id'))
return object_list
# class RegionalSitelist(ProjectRoleMixin, TemplateView):
# def get(self, request, *args, **kwargs):
# queryset = Site.objects.filter(project_id=self.kwargs.get('pk'), is_survey=False, is_active=True)
# if self.kwargs.get('region_id') == "0":
# object_list = queryset.filter(region=None)
# else:
# object_list = queryset.filter(region_id=self.kwargs.get('region_id'))
# if self.kwargs.get('region_id') == "0":
# return render(request, 'fieldsight/site_list.html',{'object_list':object_list, 'project_id':self.kwargs.get('pk'),'type':"Unregioned", 'pk': self.kwargs.get('pk'),'region_id':self.kwargs.get('region_id'),})
# obj = get_object_or_404(Region, id=self.kwargs.get('region_id'))
# return render(request, 'fieldsight/site_list.html',{'object_list':object_list, 'obj':obj, 'type':"region", 'pk': self.kwargs.get('pk'), 'region_id':self.kwargs.get('region_id'),})
# class RegionalSitelist(ProjectRoleMixin, ListView):
# paginate_by = 10
# def get(self, request, *args, **kwargs):
# if self.kwargs.get('region_pk') == "0":
# sites = Site.objects.filter(region=None)
# return render(request, 'fieldsight/site_list.html' ,{'all_sites':sites, 'project_id':self.kwargs.get('pk'),'type':"Unregioned",'pk':self.kwargs.get('region_pk'),})
# obj = get_object_or_404(Region, id=self.kwargs.get('region_pk'))
# sites = Site.objects.filter(region_id=self.kwargs.get('region_pk'))
# return render(request, 'fieldsight/site_list.html',{'all_sites':sites, 'obj':obj, 'type':"region",'pk':self.kwargs.get('region_pk'),})
示例3: post
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def post(self, request, *args, **kwargs):
self.object = self.get_object()
# Make sure the ListView gets setup
self.get(self.request, *self.args, **self.kwargs)
# Check form validity
transaction_form = self.get_transaction_form()
leg_formset = self.get_leg_formset()
if transaction_form.is_valid() and leg_formset.is_valid():
return self.form_valid(transaction_form, leg_formset)
else:
return self.form_invalid(transaction_form, leg_formset)
示例4: get_context_data
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
context['panel_title'] = _('New Topics')
context['title'] = _('Index')
context['show_order'] = True
return context
示例5: get_context_data
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def get_context_data(self, *, object_list=None, **kwargs): # pylint: disable=unused-argument
"""Overriding get_context_data to add additional context."""
context = super(ListView, self).get_context_data(**kwargs)
# Provides the base template to extend from
context['template_extends'] = self.template_extends
return context
示例6: get
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def get(self, request, *args, **kwargs):
return generic.ListView.get(self, request, *args, **kwargs)
示例7: get_context_data
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
self.generate_product_version_data(context)
return context
示例8: get_context_data
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def get_context_data(self, **kwargs):
"""
在视图函数中将模板变量传递给模板是通过给 render 函数的 context 参数传递一个字典实现的,
例如 render(request, 'blog/index.html', context={'post_list': post_list}),
这里传递了一个 {'post_list': post_list} 字典给模板。
在类视图中,这个需要传递的模板变量字典是通过 get_context_data 获得的,
所以我们复写该方法,以便我们能够自己再插入一些我们自定义的模板变量进去。
"""
# 首先获得父类生成的传递给模板的字典。
context = super(IndexView, self).get_context_data(**kwargs)
# 父类生成的字典中已有 paginator、page_obj、is_paginated 这三个模板变量,
# paginator 是 Paginator 的一个实例,
# page_obj 是 Page 的一个实例,
# is_paginated 是一个布尔变量,用于指示是否已分页。
# 例如如果规定每页 10 个数据,而本身只有 5 个数据,其实就用不着分页,此时 is_paginated=False。
# 关于什么是 Paginator,Page 类在 Django Pagination 简单分页。
# 由于 context 是一个字典,所以调用 get 方法从中取出某个键对应的值。
paginator = context.get('paginator')
page = context.get('page_obj')
is_paginated = context.get('is_paginated')
# 调用自己写的 pagination_data 方法获得显示分页导航条需要的数据,见下方。
pagination_data = self.pagination_data(paginator, page, is_paginated)
# 将分页导航条的模板变量更新到 context 中,注意 pagination_data 方法返回的也是一个字典。
context.update(pagination_data)
# 将更新后的 context 返回,以便 ListView 使用这个字典中的模板变量去渲染模板。
# 注意此时 context 字典中已有了显示分页导航条所需的数据。
context['category'] = self.big_slug
return context
示例9: question_list_view
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def question_list_view():
class DummyView(mixins.ProjectMixin, ListView):
model = question_models.Question
return DummyView.as_view()
示例10: test_views_have_correct_parent_class
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def test_views_have_correct_parent_class(self):
self.assertEquals(self.list_view.__bases__[-1].__name__, "ListView")
self.assertEquals(self.create_view.__bases__[-1].__name__, "CreateView")
self.assertEquals(self.update_view.__bases__[-1].__name__, "UpdateView")
self.assertEquals(self.delete_view.__bases__[-1].__name__, "DeleteView")
self.assertEquals(self.detail_view.__bases__[-1].__name__, "DetailView")
示例11: get_queryset
# 需要导入模块: from django.views import generic [as 别名]
# 或者: from django.views.generic import ListView [as 别名]
def get_queryset(self):
"""
Gets queryset for ListView to return to view
"""
# Filter by program (self.program set in test_func())
financial_aids = FinancialAid.objects.filter(
tier_program__program=self.program
)
# Filter by status
self.selected_status = self.kwargs.get("status", None)
if self.selected_status is None or self.selected_status not in FinancialAidStatus.ALL_STATUSES:
self.selected_status = self.default_status
financial_aids = financial_aids.filter(status=self.selected_status)
# Filter by search query
self.search_query = self.request.GET.get("search_query", "")
search_query = reduce(
lambda q, term: (
q |
Q(user__profile__first_name__icontains=term) |
Q(user__profile__last_name__icontains=term)
),
self.search_query.split(),
Q()
)
if search_query:
financial_aids = financial_aids.filter(search_query)
# Annotate with adjusted cost
self.course_price = self.program.price
financial_aids = financial_aids.annotate(adjusted_cost=self.course_price - F("tier_program__discount_amount"))
# Sort by field
self.sort_field = self.request.GET.get("sort_by", self.default_sort_field)
if self.sort_field.startswith("-"):
self.sort_field = self.sort_field[1:]
# Defined above: self.sort_direction = ""
self.sort_direction = "-"
if self.sort_field not in self.sort_fields:
self.sort_field = self.default_sort_field
self.sort_direction = ""
financial_aids = financial_aids.order_by(
"{sort_direction}{sort_field}".format(
sort_direction=self.sort_direction,
sort_field=self.sort_field_mappings.get(self.sort_field, self.sort_field)
)
)
return financial_aids