Python django.views.generic.ListView() Examples

The following are 17 code examples of django.views.generic.ListView(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.views.generic , or try the search function .
Example #1
Source File: views.py    From django-generic-scaffold with MIT License 6 votes vote down vote up
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 
Example #2
Source File: views.py    From blog with Apache License 2.0 6 votes vote down vote up
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 
Example #3
Source File: transactions.py    From django-hordak with MIT License 5 votes vote down vote up
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) 
Example #4
Source File: tests.py    From django-generic-scaffold with MIT License 5 votes vote down vote up
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") 
Example #5
Source File: tests.py    From django-generic-scaffold with MIT License 5 votes vote down vote up
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") 
Example #6
Source File: test_mixins.py    From adhocracy4 with GNU Affero General Public License v3.0 5 votes vote down vote up
def question_list_view():
    class DummyView(mixins.ProjectMixin, ListView):
        model = question_models.Question
    return DummyView.as_view() 
Example #7
Source File: views.py    From crash with Mozilla Public License 2.0 5 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        self.generate_product_version_data(context)
        return context 
Example #8
Source File: views.py    From hknweb with MIT License 5 votes vote down vote up
def get(self, request, *args, **kwargs):
        return generic.ListView.get(self, request, *args, **kwargs) 
Example #9
Source File: views.py    From hknweb with MIT License 5 votes vote down vote up
def get(self, request, *args, **kwargs):
        return generic.ListView.get(self, request, *args, **kwargs) 
Example #10
Source File: abstract.py    From django-flexible-subscriptions with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #11
Source File: views.py    From niji with MIT License 5 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        context['title'] = context['panel_title'] = _('Search: ') + self.kwargs.get('keyword')
        context['show_order'] = True
        return context 
Example #12
Source File: views.py    From niji with MIT License 5 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        context['user'] = User.objects.get(pk=self.kwargs.get('pk'))
        context['panel_title'] = context['title'] = context['user'].username
        return context 
Example #13
Source File: views.py    From niji with MIT License 5 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        current = Topic.objects.visible().get(pk=self.kwargs.get('pk'))
        current.increase_view_count()
        context['topic'] = current
        context['title'] = context['topic'].title
        context['node'] = context['topic'].node
        context['form'] = ReplyForm()
        return context 
Example #14
Source File: views.py    From niji with MIT License 5 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        context['node'] = node = Node.objects.get(pk=self.kwargs.get('pk'))
        context['title'] = context['panel_title'] = node.title
        context['show_order'] = True
        return context 
Example #15
Source File: views.py    From niji with MIT License 5 votes vote down vote up
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 
Example #16
Source File: views.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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'),}) 
Example #17
Source File: views.py    From micromasters with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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