Could it be easier to integrate Bootstrap into ModelForms?

This is more food for thought, but I think it’s pretty relevant overall for making it easier to get up and running in Django.

At $work I use Floppyforms, which basically says “don’t import from django.forms, instead import through our own re-exports to get all the goodies”.

I’m working on a fresh Django project, working with forms, and want to use Bootstrap. But I also want to just use ModelForms without having to mess around making everything explicit (like having the right CSS applied to widgets).

Right now I am resolving this like with floppyforms, by writing my own model fields that wrap the Django ones, updating their formfields with my custom form field (wrapping the base ones from Django), and then having custom widgets that apply the right CSS.

But honestly, it would be nice to not have to do any of this and just use the stuff out of the box.

I know that I can override the HTML templates used for widgets as built-ins. I suppose I could also monkeypatch the Widget classes to my liking. But it would be cool if Django, out of the box, was like “here are N different ways you can edit form fields across the board in your app”. Similarly to how we have pluggable models for users.

Has anyone else thought about this topic and have any good insight on this?

In a way, Crispy Forms already does this through their template packs. Have you looked at it? (If you haven’t, I suggest you do to see how close it is to what you’re proposing.)

If you have tried it, and it does’t address what you’re thinking of, can you explain how your idea is different?

I don’t see something like this being added to core - I wouldn’t see the effort involved in staying up-to-date with the variety of available frameworks as being worthwhile. (Not everyone uses, or even likes, bootstrap. A number of people that I respect and whose opinions I value highly have switched to tailwind. That’s just one example. Even the different versions of bootstrap would make this a non-trivial item. Then on top of that is the necessity of providing sufficient installation instructions for those frameworks to make them work “out of the box”.)

Yeah I had looked over crispy forms, but was unsatisfied at having to use the helper object. Granted, looking at how to integrate with ModelForm (here) it seems pretty lightweight as a wrapper.

I am not really proposing that Bootstrap be handled by Django, as much as I think it would be nice if we could easily modify the base behavior of widgets and the like, in the same way that we have a pluggable user model, or we have configurable templates.

Why? Because right now it seems like the recommended way to integrate a design system is to subclass into custom widgets and add your modifications, then use those widgets everywhere (meaning you can’t just use the default ModelForm derivations).

Maybe this is just “be explicit all over your ModelForms” but all of the Django stuff is nice.

Then again, in “real projects” I’m pretty serious about wrapping everything and not using Django models/classes directly because you end up wanting to override something at one point/restricting behavior anyways, and then you’re fighting the framework…

“I want all my forms, when rendering text fields, to use a textarea with this specific CSS class added” feels like something that one could reasonably expect.

Consider django-widget-tweaks · PyPI
It is my personal favorite, because it is the least intrusive solution that I could find. Other packages are more powerful, but personally I prefer to keep extra layers as few and thin as possible.

In the absence of specific implementation issues, it’s really tough for me to comment on this. Overriding widgets is not your only option. Replacing standard templates is also an option, along with other packages performing similar functions.

You can replace the textarea template with a template containing that css class as an attribute. (django.forms.widgets.textarea)

1 Like