如何用形式分页

时间:2019-02-04 12:18:46

标签: django django-models django-forms django-templates django-views

我想对以下观点进行分页。我怎么给它,因为它也有条件。我没有如何处理它。请帮助我在此JobListView中集成分页

class JobListView(LoginRequiredMixin, generic.TemplateView):
    template_name = 'jobs/job.html'

    def get(self, request, *args, **kwargs):
        context = super(JobListView, self).get_context_data(**kwargs)

        if 'status' in request.GET:
            form = JobSearchForm(request.GET)

            if form.is_valid():
                status = form.cleaned_data['status']
                start_date = form.cleaned_data['start_date']
                end_date = form.cleaned_data['end_date']
                company = self.request.user.userprofile.user_company

                lookups = (Q(job_company=self.request.user.userprofile.user_company)) 

                if start_date:
                    lookups = lookups & Q(job_created_on__gte=start_date)
                if end_date:
                    lookups = lookups & Q(job_created_on__lte=end_date)
                if status == 'Active':
                    lookups = lookups & Q(job_status=status)
                if status == 'Inactive':
                    lookups = lookups & Q(job_status=status)

                jobs=Jobs.objects.exclude(job_is_deleted = True).filter(lookups)

        else:
            form = JobSearchForm()
            company_name = self.request.user.userprofile.user_company
            jobs = Jobs.objects.exclude(
                                job_is_deleted = True
                            ).filter(
                                job_company=self.request.user.userprofile.user_company
                            )

        return render(request, self.template_name, {'form': form, 'jobs': jobs}) 

1 个答案:

答案 0 :(得分:2)

class JobListView(LoginRequiredMixin, generic.ListView):
    template_name = 'jobs/job.html'
    form_ = JobSearchForm()
    paginate_by = 5
    context_object_name = 'jobs'

    def get_queryset(self, *args, **kwargs):
        if 'status' in self.request.GET:
            self.form_ = JobSearchForm(self.request.GET)
            jobs = Jobs.objects.none()

            if self.form_.is_valid():
                status = self.form_.cleaned_data['status']
                start_date = self.form_.cleaned_data['start_date']
                end_date = self.form_.cleaned_data['end_date']
                company = self.request.user.userprofile.user_company

                lookups = (Q(job_company=self.request.user.userprofile.user_company)) 

                if start_date:
                    lookups = lookups & Q(job_created_on__gte=start_date)
                if end_date:
                    lookups = lookups & Q(job_created_on__lte=end_date)
                if status == 'Active':
                    lookups = lookups & Q(job_status=status)
                if status == 'Inactive':
                    lookups = lookups & Q(job_status=status)

                jobs = Jobs.objects.exclude(job_is_deleted = True).filter(lookups)
        else:
            company_name = self.request.user.userprofile.user_company
            jobs = Jobs.objects.exclude(
                                job_is_deleted = True
                            ).filter(
                                job_company=self.request.user.userprofile.user_company
                            )
        return jobs


    def get_context_data(self, *args, **kwargs):
        context = super(JobListView, self).get_context_data(**kwargs)
        context["form"] = self.form_
        return context

您可以继承ListView并使用paginate_by

如果您需要使用jobs = Jobs.objects.all()而不是jobs = Jobs.objects.none()来默认所有数据,则使用IF (OBJECT_ID('Con_First') IS NOT NULL) BEGIN ALTER TABLE Customer DROP CONSTRAINT Con_First; END 代替无效的查询集(如果表单无效)。这正在工作