搜索过滤器在分页Django中不起作用

时间:2019-02-19 10:08:57

标签: django django-models django-rest-framework django-views

我正在使用listviewSearchForm中进行搜索。即使我在我的代码中添加了分页功能,也可以正常工作。 但是问题是,当我搜索然后可以正常工作时,问题是当我转到分页的第二页时,搜索过滤器将被擦除,并且没有过滤器残留

Views.py

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

    def get_queryset(self, *args, **kwargs):
        if 'status' in self.request.GET:
            self.form_ = JobSearchForm(self.request.GET, instance=self.instance)
            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

Forms.py

ACTIVE_CHOICES = (
    ('AllStatus', 'Select Status'),
    ('Active', 'Active'),
    ('Inactive', 'Inactive'),
)
class JobSearchForm(forms.Form):
    start_date = forms.DateField(label=False)
    end_date = forms.DateField(label=False)
    status = forms.ChoiceField(choices=ACTIVE_CHOICES, label=False, initial="")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['start_date'].widget.attrs['placeholder'] = 'Start Date'
        self.fields['end_date'].widget.attrs['placeholder'] = 'End Date'
        # self.fields['status'].initial = 'Select Status'
        self.fields['start_date'].widget.attrs['class'] = 'job_date'
        self.fields['end_date'].widget.attrs['class'] = 'job_date'
        self.fields['start_date'].required=False
        self.fields['end_date'].required=False

        self.helper = FormHelper(self)
        self.helper.layout = Layout(
            Row(
                Column('start_date', css_class='form-group col-md-2 mb-0'),
                Column('end_date', css_class='form-group col-md-2 mb-0'),
                Column('status', css_class='form-group col-md-2 mb-0'),
                Column(Submit('submit', 'Submit'), css_class='form-group col-md-2 mb-0'),
                css_class='form-row'
            ),            

        )

请参阅此图像以供参考 enter image description here

0 个答案:

没有答案