在我的.views文件中添加了一个新的def函数之后,我仍然遇到此错误(“函数”对象不可迭代),对此问题有何想法?
目标是使用复选框过滤查询集。
这是我的views.py函数:
def FilterView(request):
qs = Product.objects.all()
ptag = request.GET.get('ptag')
if ptag == 'on':
qs = qs.filter(ptag='')
qs = filter(request)
context = {
'queryset': qs
}
return render(request, "partials/search_form.html", context)
在我的网址中:
from search.views import HomeView, FilterView
urlpatterns = [
url(r'^$', HomeView.as_view(), FilterView),
]
非常感谢!
答案 0 :(得分:1)
您的代码没有意义。您要将HomeView
和FilterView
都传递给单个url()
,并且正在请求对象上运行内置的filter
函数吗?这应该是一个有效的示例,减去无法解释的filter()
:
urls.py
from search.views import HomeView, FilterView
urlpatterns = [
...
url(r'^$', FilterView, name='filter'),
...
]
views.py
def FilterView(request):
ptag = request.GET.get('ptag', '')
qs = Product.objects.all() if ptag != 'on' else Product.objects.filter(ptag='')
#qs = filter(request) # What is this even trying to do?
context = {
'queryset': qs
}
return render(request, "partials/search_form.html", context)
答案 1 :(得分:1)
您同时传递了homeview和filterview。您需要选择一个。
在urls.py中 改变
url(r'^$', HomeView.as_view(), FilterView)
到
url(r'^$', Filterview)