1

Trying to use Django-filter class based view FilterView but I have problems with rendering filter in the template. It seems the filter is not in contex.

DOCS: https://django-filter.readthedocs.io/en/master/guide/usage.html#generic-view-configuration

EDIT: It seems that the get(self...) function of the view is never being called. I've set breakpoints in get method and it didn't stopped there.

EDIT2: It's really weird. Debugger doesn't stop even on line: class UserFilterView(...)

filters.py

class UserProfileFilter(django_filters.FilterSet):
    class Meta:
        model = UserProfile
        fields = ['budget','looking_for','user']

views.py

class UserFilterView(FilterView):
    filterset_class = UserProfileFilter
    template_name = 'frontend/userprofile_filter.html'

userprofile_filter.html

{% extends "frontend/base.html" %}
{% block content %}
    <div class="filter">
        {{ filter.form.as_p }}
    </div>
    <hr>
    <div class="results">
    </div>
{% endblock %}

But in browser, I see:

...
<div class="filter">

</div>
...

urls.py

url('^search/$',views.UserFilterView.as_view(), name="user_filter")

Can't figure out where is the problem. Do you have any ideas?

Milano
  • 18,048
  • 37
  • 153
  • 353

1 Answers1

1

It sounds like you have some kind of issue with inheritance of your CBV that is tripping you up, due to a get method defined elsewhere. We'd probably need to see more code, plus exact versions of everything, to debug.

However, I strongly recommend you sidestep all of this by writing the 3 lines view here - https://django-filter.readthedocs.io/en/master/guide/usage.html#the-view

CBVs are great for taking an incredibly simple thing and turning into into something that takes hours to debug.

spookylukey
  • 6,380
  • 1
  • 31
  • 34