在基于类的通用视图中使用request.POST数据的动态查询集

时间:2011-11-21 18:52:52

标签: django django-forms django-queryset django-class-based-views

我对如何使用request.POST查询字典制作动态查询集感兴趣?当我这样做时:

class ListCv(ListView):
    queryset = CV.objects.all()
    template_name = 'jobseek/applicants_resumes_list.html'  
    paginate_by = 5

    def get_context_data(self, **kwargs):
        context = super(ListCv, self).get_context_data(**kwargs)
        context['main_form'] = FilterCV(self.request.POST or None)
        return context

    def get_queryset(self):
        request = self.request
        main_form = FilterCV(request.POST or None)

        if main_form.is_valid():
            cleaned_fields = main_form.cleaned_data                 

            query_tag = cleaned_fields.get('query_tag')            

            lookup = (~Q(position__icontains=query_tag)| 
                      ~Q(additional_information__icontains=query_tag)|
                      ~Q(title__icontains=query_tag))
            return CV.objects.filter(lookup)
        return CV.objects.all()

我有空白页面,没有任何例外。我可以使用FormMixin,ProcessFormView来解决我的问题吗?如果我可以,怎么样?

修改

模板:

<div class="vacancies_list">
    {% for object in object_list %}               
        <div class="vacancy">
           <div class="title"><a href="{% url view_cv object.id %}" title="">{{     object.title }}</a></div>
           <div class="body">
           {{ object.employment_type }}; 
           {% for education in object.highereducation_set.all %}
           {{ education.faculty }}: {{ education.specialty }}
           {% endfor %}
           {% for experience in professionalexperience_set.all %}
           {{ experience.company_name }}: {{ experience.position }}
           {% endfor %}...
           &nbsp;<a href="{% url view_cv object.id %}" title="" class="more">Полное     описание<img src="{{ STATIC_URL }}images/arrow_right_green.png" alt=">" title="" /></a>
           </div>
        </div>
     {% endfor %}       
  </div>

0 个答案:

没有答案