django-crispy-forms / crispy-tailwind

A Tailwind template pack for django-crispy-forms
MIT License
331 stars 56 forks source link

AttributeError with unknown widgets #73

Closed jeverling closed 3 years ago

jeverling commented 3 years ago

Hi, many thanks for this package! I'm using GooglePointFieldWidget from https://github.com/erdem/django-map-widgets

This results in an AttributeError in tailwind.py#L72:

'CSSContainer' object has no attribute 'googlepointfield'

because googlepointfield is not contained in default_items.

Just setting a fallback for getattr, e.g. getattr(self, widget_name, widget_name) seems to make it work.

toudi commented 3 years ago

I had a similar problem today and for a moment I was hoping that I could override the CSSContainer in the template context. Sadly, that was not possible as context is merely some local context of the theme template. However what I ended up doing was the following: inside one of my apps (called system) I added the following startup code:

from django.apps import AppConfig
from crispy_tailwind.templatetags.tailwind_field import CrispyTailwindFieldNode

class SystemConfig(AppConfig):
    name = "apps.system"

    def ready(self) -> None:
        setattr(CrispyTailwindFieldNode.default_container, "datepicker", "")
        return super().ready()

there are some operator overloads in CSSContainer class but it's already late and I did not quite understood what they were doing. I used a datepicker but I'm sure you get the idea.

of course this is just a monkey patch, but I couldn't think of a better solution for the time being and forking the whole project just to override this single function seemed like a massive overkill.

cheers.