1

目前,当我的用户注册并出现错误时,我会收到以下消息:

status 400
{"username":["This field is required."],"email":["A user is already registered with this e-mail address."],"__all__":["You must type the same password each time."]}

这是针对我的移动应用程序的,我希望在返回错误时得到一致的消息(不是特定于字段的,如“用户名”、“电子邮件”)。例如,

status 400
{"error_message":"username - This field is required. email - A user is already registered with this e-mail address. __all__ - You must type the same password each time."}

无论如何我可以扩展注册/视图/注册类吗?

4

1 回答 1

3

您需要覆盖该RegisterView.get_response_with_errors方法。

class MyRegisterView(RegisterView):
    def get_response_with_errors(self):
        errors = my_magic_func(self.form.errors)
        return Response(errors, status=status.HTTP_400_BAD_REQUEST)

my_magic_func根据错误字典自定义错误消息的技巧在哪里。然后将自定义视图连接到您的网址中:

url(r'^rest-auth/registration/$', MyRegisterView.as_view(), name='rest_register'),

rest-auth记得在任何相关网址之前添加它。

于 2015-10-04T18:46:44.013 回答