0

I keep getting this error ('function' object is not iterable) after having added a new def function in my .views file, any thoughts on what the problem could be?

The goal with this is to filter querysets with checkboxes.

Here's my views.py function:

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)

And in my urls:

from search.views import HomeView, FilterView

urlpatterns = [
    url(r'^$', HomeView.as_view(), FilterView),
]

Thanks so much!

5
  • 1
    please include your whole stack trace Commented Apr 29, 2019 at 0:54
  • What is qs = filter(request) supposed to do? Commented Apr 29, 2019 at 1:08
  • Why are you using url(...) in urlpatterns? I believe path(...) is recommended. Commented Apr 29, 2019 at 1:14
  • Why are you passing both HomeView.as_view() and FilterView as arguments to your root url? Commented Apr 29, 2019 at 1:15
  • Thank you for your answers, really appreciated! And sorry guys, I'm still new to this, but willing to learn. :) I'm using url(...) instead of path(...) because I'm on Django 1.10.5 version since I need to use Haystack and this urlpattern comes with it as default. Commented Apr 29, 2019 at 15:57

2 Answers 2

1

Your code doesn't make sense. You are passing both your HomeView and your FilterView to a single url(), and you are running the builtin filter function on a request object? Here should be a working example, minus the unexplained 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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much for your answer, I really appreciate that! What about inserting this function inside a class like this? I've tried it and I don't get the error anymore! Here's the urls: url(r'^$', HomeView.as_view()
And the code in the views file: ` class HomeView(TemplateView): template_name = "home.html" def FilterView(request): ptag = request.GET.get('ptag', '') qs = Product.objects.all() if ptag != 'on' else Product.objects.filter(ptag='') context = { 'queryset': qs } return render(request, "partials/search_form.html", context)`
1

You are passing in both homeview and filterview. You need to choose one.

in urls.py change

url(r'^$', HomeView.as_view(), FilterView)

to

url(r'^$', Filterview)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.