我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用django.views.generic.ListView()。
def get_context_data(self, **kwargs): context = super(ListView, self).get_context_data(**kwargs) context['prefix'] = self.prefix context['list_header'] = self.get_list_header() context['list_items'] = self.get_list_items(context['object_list']) context['tool_links'] = self.get_tool_links() # self.has_action_links is set in get_list_items context['has_action_links'] = self.has_action_links context['tool_links_icon'] = self.get_tool_links_icon() if self.get_simple_search_form(): context['simple_search_form'] = \ self.get_simple_search_form()(data=self.request.GET) if self.get_advanced_search_form(): context['advanced_search_form'] = \ self.get_advanced_search_form()(data=self.request.GET) return context
def test_search(self): test = SampleModel.objects.create(title='test') foobar = SampleModel.objects.create(title='foobar') bar = SampleModel.objects.create(title='bar') class SampleModelListView(SmartListMixin, ListView): model = SampleModel list_display = ('title', 'category') search_fields = ('title', ) request = self.factory.get('/smart-lists/?q=test') view = SampleModelListView(request=request) self.assertEqual(1, len(view.get_queryset())) self.assertEqual(test, view.get_queryset()[0]) request = self.factory.get('/smart-lists/?q=bar') view = SampleModelListView(request=request) self.assertEqual(2, len(view.get_queryset())) self.assertEqual([foobar, bar], list(view.get_queryset()))
def dispatch(self, request, *args, **kwargs): """Overriding dispatch on ListView""" self.model = get_model(**kwargs) return super(EntityList, self).dispatch( request, *args, **kwargs)
def get_queryset(self): queryset = [] for category in super(ListView, self).get_queryset(): subQueryset = [] for criterion in category.criteria.all(): subQueryset.append( (criterion, get_weight(criterion, self.request.user)) ) queryset.append((category, subQueryset)) return queryset
def get_context_data(self, **kwargs): context = super(ListView, self).get_context_data(**kwargs) context['total_criteria'] = Criterion.objects.count() context['group_form'] = GroupForm(data=self.request.GET) group_id = self.request.GET.get('group') if group_id: try: group_id = int(group_id) except (ValueError, TypeError): pass else: context['group'] = get_object_or_404(Group, pk=self.request.GET['group']) return context
def get(self, request, *args, **kwargs): self.option = get_object_or_404(Option, uuid=self.kwargs['option_uuid']) return super(ListView, self).get(request, *args, **kwargs)
def get_queryset(self): # Querset will be a list of tuples in the form (Criterion, Score) queryset = [] for category in super(ListView, self).get_queryset(): subQueryset = [] for criterion in category.criteria.all(): subQueryset.append( (criterion, get_score(criterion, self.option, self.request.user)) ) queryset.append((category, subQueryset)) return queryset
def get_context_data(self, **kwargs): context = super(ListView, self).get_context_data(**kwargs) context.update( option=self.option, ) return context
def question_list_view(): class DummyView(mixins.ProjectMixin, ListView): model = question_models.Question return DummyView.as_view()
def test_ordering_in_queryset(self): class SampleModelListView(SmartListMixin, ListView): model = SampleModel list_display = ('title', 'category') request = self.factory.get('/smart-lists/') view = SampleModelListView(request=request) self.assertEqual(view.get_ordering(), []) request = self.factory.get('/smart-lists/?o=1') view = SampleModelListView(request=request) self.assertEqual(view.get_ordering(), ['title']) request = self.factory.get('/smart-lists/?o=2.1') view = SampleModelListView(request=request) self.assertEqual(view.get_ordering(), ['category', 'title']) request = self.factory.get('/smart-lists/?o=-2.1') view = SampleModelListView(request=request) self.assertEqual(view.get_ordering(), ['-category', 'title']) request = self.factory.get('/smart-lists/?o=-wqdwd') view = SampleModelListView(request=request) self.assertRaises( SmartListException, view.get_ordering ) request = self.factory.get('/smart-lists/?o=5') view = SampleModelListView(request=request) self.assertRaises( SmartListException, view.get_ordering )
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)
def project_detail_view(): class FakeProjectDetailView(mixins.PhaseDispatchMixin, ListView): model = models.Project def get(self, request, *args, **kwargs): return HttpResponse('project_detail') return FakeProjectDetailView.as_view()
def dummy_view(): class DummyView(mixins.ProjectPhaseMixin, ListView): model = models.Project return DummyView.as_view()
def recherche(request): user = User.objects.all() return render(request, 'user/users.html', {'liste_user': user}) #class UserList(ListView): # template_name = 'user/search.html' # # def get_queryset(self): # query = self.request.GET.get('q') # return User.objects.filter(username=query)
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