Hi there,
I am new to Django and I’m working on a project which is basically a point of sale and I am using sweetify library which adds support for sweetalerts js library. I don’t know how to display a user the error message. I mean we can simply show a “Success” message but how can I display the error message which actually helps the user to understand what’s wrong with their submission. I’ll appreciate your kind suggestions
Are you using sweetify-django
? If so, the docs only indicate a successful form post integration via SuccessMessageMixin
. It does indicate that you can render error messages via:
sweetify.error(self.request, 'Some error happened here - reload the site', persistent=':(')
You could create your own mixin to handle invalid form submissions. Take a look at the implementation of SuccessMessageMixin
for inspiration. You’ll need to wire into the form_invalid
method.
Perhaps something like:
class SweetifyErrorMixin(object):
error_message = ''
sweetify_options = {}
def form_invalid(self, form):
response = super(SweetifyErrorMixin, self).form_valid(form)
message = self.get_error_message(form.errors)
if message:
sweetify.error(self.request, message, **self.get_sweetify_options())
return response
def get_error_message(self, errors):
return self.error_message % errors
def get_sweetify_options(self):
return self.sweetify_options
I’m not sure how the form.errors
will work. You may need to adjust things. Here are the docs regarding that property in particular.