django form invalid mixin

Django -- Posted on Jan. 21, 2021

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)
                  
   
            

Related Posts