首页 文章

在django-registration上自定义错误消息

提问于
浏览
0

我有一个关于django-registration(https://bitbucket.org/ubernostrum/django-registration)的问题,但我可以't find their issue tracker or a mailing list for it, so I'在这里试试我的运气 .

我的应用程序允许通过OpenID和登录/密码登录 .

FS上的一些用户"forget their password"尝试重置它(here),但随后他们收到消息:

与此电子邮件地址关联的用户帐户无法重置密码 .

没有进一步的解释 . (您可以尝试重置我的密码 - 只需在那里输入我的电子邮件(tonylampada at gmail dot com)即可查看错误消息 .

我想自定义该消息 . 更好的信息是:

与此电子邮件地址关联的用户帐户无法重置密码 . 发生这种情况是因为用户帐户是使用OpenID或OAuth提供程序(简称Google,Facebook,MyOpenID等)创建的 . 要查看与此帐户关联的登录提供程序,请查看用户配置文件 .

告诉django-registration最简单的方法是什么?

谢谢!

PS:关于Github的这个问题:https://github.com/freedomsponsors/www.freedomsponsors.org/issues/191(以防万一你今天想要提出拉动请求:-))

1 回答

  • 2

    django-registration使用 django.contrib.auth 中的视图 .

    在这种情况下: reset_password() github

    由于这不是基于类的视图,因此您无法覆盖/继承它,但您可以从django.contrib.auth.forms传入PasswordResetForm .

    from django.contrib.auth.forms import PasswordResetForm
    
    class CustomResetForm(PasswordResetForm):
    
           def validate(self, value):
                #pseudocode
                if user.cant_reset_pw:
                   raise ValidationError("The user account associated with this e-mail address cannot reset the password. and so forth..")
    
                super(CustomResetForm, self).validate(value)
    

    您必须通过覆盖网址 r'^password/change/$' 来指向一个自定义函数,并使用 CustomResetForm 调用 django.contrib.auth.passwort_reset() .

相关问题