在用户注册时发送密码django-allauth [英] Send password on user registration, django-allauth

查看:58
本文介绍了在用户注册时发送密码django-allauth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在django应用程序上使用django-alluth进行身份验证/注册.而且我需要创建一个仅包含一个字段的自定义注册表单:电子邮件.密码将在服务器上生成.这是我创建的表单:

I am using django-alluth for authentication/registration on my django app. And I need to create a custom signup form with only one field: email. The password will be generated on the server. Here is a form I have created:

from django import forms
from users.models import User


class SignupForm(forms.ModelForm):
    email = forms.CharField()

    class Meta:
        model = User
        fields = ['email',]
        esclude = ['password1', 'password']

    def __init__(self, *args, **kwargs):
        super(SignupForm, self).__init__(*args, **kwargs)
        del self.fields['password1']
        del self.fields['password2']

    def signup(self, request, user):
        password = str(User.objects.make_random_password())
        user.set_password(password) #!!!!!!! HERE 
        user.save()

在settings.py中:

In settings.py:

ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.SignupForm'

现在,我需要向用户发送带有密码的电子邮件.当然,我可以使用 signup 方法执行此操作,但这不是一个好习惯,因为仅需要一种表格来收集和检查信息.

And now I need to send an email to the user with his password. Of course, I can do this in signup method, but it is not good practice, as a form is needed only for gathering and checking information.

监听 user_signed_up django-allauth信号会很好,但是由于密码存储在哈希中,因此无法在其中获取用户密码.有什么办法可以传递其他参数来发信号吗?也许还有其他发送电子邮件的方法?

It will be good to listen to user_signed_up django-allauth signal, but it will be impossible to get user password in it, as a password is stored in a hash. Is there any way to pass additional parameters to signal? Or maybe there are some others methods to send an email?

非常感谢.

推荐答案

如果您不想在signup方法中执行此操作,请不要在表单内部进行注册,而要在视图中进行注册.

If you don't want to do it in the signup method, don't do the signup inside the form, but in the view instead.

只有在创建密码并将其提供给用户时,您才可以访问密码,这就是您需要将密码发送给该用户的时候.

The only moment you have access to the password is right when you create it and give it to a user, so that's when you need to send it to said user.

这篇关于在用户注册时发送密码django-allauth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆