0

我正在使用自定义用户模型,到目前为止,我已经能够将它连接到扩展“SignupForm”的 allauth 包中。

一切似乎都运行良好,因为我注册了并且有关新帐户的信息在数据库中(用户表和 account_emailaddress 表),但未发送电子邮件。

这是我的注册表单的样子

表格.py

class StudentSignUpForm(SignupForm): 

    @transaction.atomic
    def save(self, request):
        user = super(StudentSignUpForm, self).save(request)
        user.is_student = True
        user.save()
        student = StudentProfile.objects.create(user=user)
        return user

视图.py

def student_profile_view(request):

    if request.method == 'POST':

        user_form = StudentSignUpForm(request.POST, prefix='UF')

        if user_form.is_valid():
            user_form.save(request)
            return redirect('/') 

    else: 
        user_form = StudentSignUpForm(prefix='UF')

    return render(request, 'registration/student-profile.html', {'user_form': user_form,})

这适用于创建自定义用户以及在 All auth 下创建用户,注册的电子邮件地址显示在 accounts/emails_address 下的“/admin/”中,但不会向新注册的用户发送确认电子邮件。

我该如何解决??有任何想法吗??

4

3 回答 3

0

要发送电子邮件,您需要配置 django 电子邮件设置

请按照链接发送电子邮件和 链接以了解电子邮件的 django 设置

保存用户配置文件后,您调用

class StudentSignUpForm(SignupForm): 
    ........
    student = StudentProfile.objects.create(user=user)
    send_mail(
        'Subject here',
        'Here is the message.',
        'from@example.com',
        ['to@example.com'],
        fail_silently=False,
    )
于 2020-05-27T10:27:09.857 回答
0

setting.py中,添加:

ACCOUNT_EMAIL_VERIFICATION =”mandatory”
于 2021-12-14T22:35:06.180 回答
0

我修好了它。问题出在视图上,我需要做的就是更改为基于类的视图并使用 allauth signupview。

在views.py

from allauth.account.views import SignupView


class student_profile_view(SignupView):

    # The referenced HTML content can be copied from the signup.html
    # in the django-allauth template folder

    template_name = 'registration/student-profile.html'

    # the previously created form class
    form_class = StudentSignUpForm

然后在我的自定义注册模板中,我只是传入“表单”来呈现注册表单。

在 urls.py

    path('register/student/', student_profile_view.as_view(), name='intern_register'),

于 2020-05-28T07:41:29.910 回答