I have a field with “unique=True” in my model this is good. The error toast message has displayed successfully . When I pass (form.as_text()) to the toast message, it returns with tow messages
The first one is (“Vendor with this Name already exists.”)
And the second says (“This field is required”)
The 2 messages appear as toast message next to each other.
if request.is_ajax() and request.method == "POST":
form = VendorsForm(request.POST or None, request.FILES or None)
[print('FORM-ERRORS: ', f.name, f.errors,) for f in form]
if form.is_valid():
save_form = form.save(commit=False)
save_form.user = request.user
save_form.updated_user = request.user
name = request.POST.get("name")
# match = Vendor.objects.filter(name=name).exists()
save_form.save()
ven_name = save_form.name
ven_id = save_form.id
# messages.success(request, f"Vendor ({name}) created successfully.")
# return redirect(reverse("vendors:edit_vendor", args=(id,)))
msg = 'Vendor (%s) created successfully' % ven_name
data['error'] = msg
data['type'] = 'success'
data['vendor_id'] = ven_id
return JsonResponse(data)
else:
name = request.POST.get("name")
print(
'VALIDITY-IS: ', form.is_valid(),
'VENDOR-NAME: ', name,
form.errors.as_json(escape_html=False),
form.errors.as_data(),
'GET_JSON_DATA: ', (form.get_json_data()),
)
er = form.errors.as_json(escape_html=True)
# error_name = er["name"]
msg = (form.errors.as_text())
print(msg)
data['error'] = msg
data['type'] = 'error'
data['vendor_id'] = None
return JsonResponse(data)
else:
form = VendorsForm()
Why does the message appear twice ?
I have tried many things but I failed .
Thanks