from django.shortcuts import render
from django.views.generic import ListView
from jobs.models import Job
from django.db.models import Q
from .filters import JobFilter
class JobListView(ListView):
model = Job
paginate_by = 10
template_name = 'jobs/job_list.html'
def get_queryset(self):
queryset = Job.objects.all()
q = self.request.GET.get("q")
if q:
queryset = queryset.filter(
Q(title__icontains=q) |
Q(category__category__icontains=q) |
Q(description__icontains=q)
).distinct()
self.sort_by = self.request.GET.get('sort_by', 'start_date')
return queryset.order_by(self.sort_by)
def get_context_data(self, **kwargs):
context = super(JobListView, self).get_context_data(**kwargs)
context['title'] = "Job List"
context['sort_by'] = self.sort_by
context['object_name'] = "job_list"
return context
Everything thing works fine with this. However what I would like to do is to allow a person to filter on certain criteria within this page. I have created a separate view using django-filters which achieves this but would like to combine this. Any suggestions?