Repository: encode/django-rest-framework

Issue:
Admin and API browser fails for views without a filter_class
The JSON browser does work though. It seems crispy forms breaks because it's expecting a filter form even when there is no filter_class set on a given view.

```
VariableDoesNotExist at /api/users/

Failed lookup for key [form] in u'None'

Template error:
In template /Users/user/virtualenvs/ddrsite/lib/python2.7/site-packages/rest_framework/templates/rest_framework/filters/django_filter_crispyforms.html, error at line 5
   (Could not get exception message)

   1 : {% load crispy_forms_tags %}
   2 : {% load i18n %}
   3 : 
   4 : <h2>{% trans "Field filters" %}</h2>
   5 :  {% crispy filter.form %}
```

Admin and API browser fails for views without a filter_class
The JSON browser does work though. It seems crispy forms breaks because it's expecting a filter form even when there is no filter_class set on a given view.

```
VariableDoesNotExist at /api/users/

Failed lookup for key [form] in u'None'

Template error:
In template /Users/user/virtualenvs/ddrsite/lib/python2.7/site-packages/rest_framework/templates/rest_framework/filters/django_filter_crispyforms.html, error at line 5
   (Could not get exception message)

   1 : {% load crispy_forms_tags %}
   2 : {% load i18n %}
   3 : 
   4 : <h2>{% trans "Field filters" %}</h2>
   5 :  {% crispy filter.form %}
```



Discussion:
Ugh! Yeah that's definitely what I thought might happen with #3560. @mcastle can you confirm this is happening with the just released 3.3.1?

Update: Specifically talking about [this comment](https://github.com/tomchristie/django-rest-framework/pull/3560#issuecomment-152003241) on #3560.

Yep, this happened with 3.3.1.

Same for me with 3.3.1, upgraded for #3560

Thanks for confirming. So I think what should probably be happening here is that if there's no filter class the filter backend's `to_html()` returns None. It'd then be skipped [here](https://github.com/tomchristie/django-rest-framework/blob/c53c9eddfe1159a731c1ac879b3f72b1085b2fe9/rest_framework/renderers.py#L596).

Ugh! Yeah that's definitely what I thought might happen with #3560. @mcastle can you confirm this is happening with the just released 3.3.1?

Update: Specifically talking about [this comment](https://github.com/tomchristie/django-rest-framework/pull/3560#issuecomment-152003241) on #3560.

Yep, this happened with 3.3.1.

Same for me with 3.3.1, upgraded for #3560

Thanks for confirming. So I think what should probably be happening here is that if there's no filter class the filter backend's `to_html()` returns None. It'd then be skipped [here](https://github.com/tomchristie/django-rest-framework/blob/c53c9eddfe1159a731c1ac879b3f72b1085b2fe9/rest_framework/renderers.py#L596).


Resolving patch:
diff --git a/rest_framework/filters.py b/rest_framework/filters.py
--- a/rest_framework/filters.py
+++ b/rest_framework/filters.py
@@ -118,10 +118,9 @@ def filter_queryset(self, request, queryset, view):
 
     def to_html(self, request, queryset, view):
         filter_class = self.get_filter_class(view, queryset)
-        if filter_class:
-            filter_instance = filter_class(request.query_params, queryset=queryset)
-        else:
-            filter_instance = None
+        if not filter_class:
+            return None
+        filter_instance = filter_class(request.query_params, queryset=queryset)
         context = {
             'filter': filter_instance
         }


Test patch: