在 SuccessMessageMixin 中自定义 success_message

custoomizing success_message in SuccessMessageMixin

无法理解一件事:

在 Django 文档中说你可以在使用 SuccessMessageMixin 时做类似的事情:

success_message = "%(name)s 已成功创建"

其中(名称)是视图使用的表单的字段。

我正在尝试在 PasswordResetView 的成功消息中添加用户名。 PasswordResetView 默认有一个名为“PasswordResetForm”的表单,这个表单只有一个字段“email”,我已经使用过。 示例 --- success_message = "Email with the instructions has been sent to your email - %(email)s"

# original Django code
class PasswordResetView(PasswordContextMixin, FormView):
    email_template_name = 'registration/password_reset_email.html’
    extra_email_context = None
    form_class = PasswordResetForm   # this form

# original Django code
class PasswordResetForm(forms.Form):
    email = forms.EmailField(label=_("Email"), max_length=254)  # this field

我正在尝试以某种方式做类似的事情:

success_message = "Email with the instructions has been sent to your email - %(email)s, %(username)s"

但问题是如果此表单只有 1 个字段“电子邮件”,从哪里获取用户名?

有趣的是,PasswordResetForm 有一个名为 get_users() 的方法,我们可以从中获取用户名:


def get_users(self, email):
    """Given an email, return matching user(s) who should receive a reset.

    This allows subclasses to more easily customize the default policies
    that prevent inactive users and users with unusable passwords from
    resetting their password.
    """
    active_users = UserModel._default_manager.filter(**{
        '%s__iexact' % UserModel.get_email_field_name(): email,
        'is_active': True,
    })
    return (u for u in active_users if u.has_usable_password())

这似乎是一项有趣的任务,但我无法理解如何将它们组合在一起以便能够将用户名添加到成功消息中。

已尝试以下操作:



class PRForm(PasswordResetForm):
    custom_user = forms.CharField(max_length=30, widget=forms.HiddenInput(), show_hidden_initial=True, initial=???) # some connections to def get_users(self, email) maybe???

在默认表单的基础上新建表单,并在其中添加隐藏字段custom_usera,但我不知道在首字母中输入什么。然后我就可以在 success_message 中使用这个字段。 您如何看待这种方法,或者它完全是愚蠢的?

无论如何,如果有人有任何想法,我会很高兴听到。 我可以没有这个任务,没有问题,但是解决它对我来说似乎很有趣。

我自己想出来的:

   class PRForm(PasswordResetForm):
    def clean_email(self):
        email = self.cleaned_data['email']
        if not ExtraUser.objects.filter(email__iexact=email, is_active=True, is_activated=True).exists():
            msg = "There is no user registered with the specified E-Mail address."
            self.add_error('email', msg)
        else:
            current_user = ExtraUser.objects.get(email__iexact=email, is_active=True, is_activated=True)
            if current_user:
                self.cleaned_data["username"] = current_user
        return email

现在可以这样写 success_message:

success_message = "Dear %(username)s , email with the instructions has been sent to your email - %(email)s"

我自己发现的:

class PRForm(PasswordResetForm):
    def clean_email(self):
        email = self.cleaned_data['email']
        if not ExtraUser.objects.filter(email__iexact=email, is_active=True, is_activated=True).exists():
            msg = "There is no user registered with the specified E-Mail address."
            self.add_error('email', msg)
        else:
            current_user = ExtraUser.objects.get(email__iexact=email, is_active=True, is_activated=True)
            if current_user:
                self.cleaned_data["username"] = current_user
        return email

1) 定义当前用户

2) 将其传递给 cleaned_data,因为 Django 从表单

的清理数据中获取此 %()s 类型标签