Django允许用户使用表单过滤对象

时间:2017-12-31 20:20:38

标签: python django

所以我在模型中有对象实例,我想允许用户根据某些特征过滤它们。我设置了一个表单并添加了一些代码来在视图中处理它。提交表单时,它返回相同的模板,但使用它们设置的对象过滤器。然而它不起作用......我想知道为什么/我应该怎么做呢。

自:

class PropertySearchForm(forms.Form):
    beds = forms.CharField(widget=forms.Select(attrs={'class':'form-control mb-3'}, choices=BED_CHOICES))
    baths = forms.CharField(widget=forms.Select(attrs={'class':'form-control mb-3'}, choices=BATH_CHOICES))

查看:

def UniversityHomePageView(request,name_initials):
    university = get_object_or_404(University,name_initials=name_initials)
    listings = Listing.objects.filter(l_university=university)
    form = PropertySearchForm(request.GET or None)
    company = Company.objects.filter(c_university=university)

    context = {
            'listings':listings,
            'university':university,
            'company':company,
            'form':form,
            }

    if form.is_valid():
        beds = int(form.cleaned_data.get('beds'))
        baths = int(form.cleaned_data.get('baths'))
        listings = Listing.objects.filter(l_university=university,beds=beds,baths=baths)

        return render(request,'listings/university_homepage.html',context)

    return render(request,'listings/university_homepage.html',context)

0 个答案:

没有答案