获取form对象
form = LoginForm(request.POST)
表单验证
if form.is_valid():
pass
else:
errors = form.get_errors()
return JsonResponse({"code":400,"message":"","data":errors})
# 获取验证表单错误信息
class FormMixXin(object):
def get_errors(self):
if hasattr(self,'errors'):
errors = self.errors.get_json_data()
new_errors = {}
for key, message_dicts in errors.items():
messages = []
for message in message_dicts:
message = message['message']
messages.append(message)
new_errors[key] = messages
return new_errors
else:
return {}