carltongibson / django-filter

A generic system for filtering Django QuerySets based on user selections

Home Page:https://django-filter.readthedocs.io/en/main/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using crispy-bootstrap5 with Django-filter and DRF

TTycho opened this issue · comments

commented

FilterSet in django_filters.rest_framework.filterset.py defines the template pack to use. It looks like bootstrap3 is hardcoded here:

        if compat.is_crispy():
            from crispy_forms.helper import FormHelper
            from crispy_forms.layout import Layout, Submit

            layout_components = list(form.fields.keys()) + [
                Submit('', _('Submit'), css_class='btn-default'),
            ]
            helper = FormHelper()
            helper.form_method = 'GET'
            helper.template_pack = 'bootstrap3'
            helper.layout = Layout(*layout_components)

This breaks crispy_bootstrap5. Would it not be nicer to do?:

from django.conf import settings
        if hasattr(settings, 'CRISPY_TEMPLATE_PACK'):
            helper.template_pack = settings.CRISPY_TEMPLATE_PACK
        else:
            helper.template_pack = 'bootstrap3'

Or is there an other place to set the template pack?

Concur. Just ran into this.

I disabled crispy rendering altogether with following monkey patch in my app's ready() method:

        # monkey patch out django-filters crispy form rendering:
        from django_filters import compat
        compat.is_crispy = lambda: False

Right now you can workaround this issue by monkey patching out template rendering like above, or by subclassing FilterSet and overriding the form property.

The correct change here is trivial though, just remove the template_pack setter line altogether. I will submit a PR.