Send password on user registration, django-allauth

江枫思渺然 提交于 2021-01-27 19:09:45

问题


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()

In settings.py:

ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.SignupForm'

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.

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?

Thanks a lot.


回答1:


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.



来源:https://stackoverflow.com/questions/40586943/send-password-on-user-registration-django-allauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!