【问题标题】:Customize error messages on django-registration自定义 django-registration 上的错误消息
【发布时间】:2013-09-09 21:15:03
【问题描述】:

我有一个关于 django-registration (https://bitbucket.org/ubernostrum/django-registration) 的问题,但我找不到他们的问题跟踪器或邮件列表,所以我会在这里试试运气。

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

一些用户在 FS 上“忘记密码”尝试重置它 (here),但随后他们收到消息:

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

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

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

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

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

谢谢!

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

【问题讨论】:

    标签: python django django-registration


    【解决方案1】:

    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)
    

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

    【讨论】:

      猜你喜欢
      • 2014-07-14
      • 1970-01-01
      • 2013-05-05
      • 2011-06-23
      • 2019-12-14
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多