首页 > 解决方案 > Django:发送验证电子邮件后如何保存新的电子邮件地址

问题描述

我正在创建通过确认电子邮件验证新电子邮件地址的逻辑。但我不明白如何在发送验证电子邮件后保存新的电子邮件地址。我需要将新的电子邮件地址保存在某处,以便在用户检查验证电子邮件时保存它。验证逻辑通常如何工作?

我当前的代码是这样的

views.py

def change_email(request):
    if request.method == 'POST':
        form = ChangeEmailForm(request.POST)

        if form.is_valid():
            # send the verification email here while creating a token
            ...
            to_email = form.cleaned_data.get('new_email')
            email = EmailMessage(subject, message, to=[to_email],)
            email.send()
            return HttpResponseRedirect...


def verify_email(request, uid64, token):
    # user verify the new email address when checking the verification email here but how I can save the new email address the user input

如何保存用户输入的新电子邮件地址或将新电子邮件地址值保存在表单上以便以后保存?

标签: djangoemail

解决方案


扩展用户模型以添加一个附加字段“new_email”,然后在发送电子邮件之前将其保存。

def change_email(request):
    if request.method == 'POST':
        form1 = ChangeEmailForm(request.POST)
        if form1.is_valid():
            request.user.new_email = form.cleaned_data['new_email']
            request.user.save()
            ...

推荐阅读