Form invalid mixin for django
class FormInvalidMixin:
def form_invalid(self, form, **kwargs):
from django.contrib import messages
messages.error(self.request, f"{self.model.__name__.capitalize()} not saved.")
for field in form.errors:
try:
form[field].field.widget.attrs['class'] += ' is-invalid'
except KeyError:
form[field].field.widget.attrs['class'] = ' is-invalid'
return super().form_invalid(form)