首页 > 解决方案 > Django 渲染和提交从另一个表单传递给电子邮件验证码的表单

问题描述

我想完成以下操作,使用验证码进行电子邮件验证,而不是链接

  1. 用户在注册表单中注册
  2. 用户电子邮件作为上下文传递给验证码输入表单
  3. 用户收到验证码
  4. 用户在验证码输入表单中输入验证码
  5. 验证码在后端通过第一个表单上下文发送的电子邮件进行确认

所以我想要做的是,当用户访问example.com/signup并填写表单时,我会渲染verification_code_entry_form,然后他们在那里输入他们的验证码。最后,一旦他们提交,他们就会被重定向到登录。然而,渲染和重定向之间的区别让我很困惑。

urls.py

path('signup', views.signup_page, name='signup'),
path('confirm_account', views.confirm_account, name="confirm_account"),
path('send_verification_code', views.send_verification_code, name="send_verification_code"),

views.py

def signup_page(request):
    form = CreateUserForm(request.POST or None)
    context = {
        'form': form
    }

    if form.is_valid():
        new_user = form.save(commit=False)
        password = form.cleaned_data.get('password')
        email = form.cleaned_data.get('email')
        try:
            ...backend authentication client work goes here

        except Exception as e:
            ...catch exceptions here
            messages.error(
                request,
                f'An unexpected error has occured.'
            )
            return render(request, 'accounts/signup.html', context)
        else:
            # Create user and verifications
            new_user.save()

            # Save the email in the context to use in the verification form
            context['email'] = email
            form = AccountConfirmationForm(request.POST or None)
            context['form'] = form
            messages.success(
                request,
                f'Account succesfully created for {email}. Please verify your e-mail, then login.'
            )
            
            # Redirect (render?) to verification code formation
            return render(request, 'confirm_account.html', context)

    return render(request, 'signup.html', context)


def confirm_account_verification_code(request):

    if not request.get('email'):
        """
        This represents a failure of the client to properly navigate to the verification code from signup or login
        So we should prompt them to reset their verification code and re-render with context
        """
        messages.error(request, f"An error occurred when processing your verification code request. Please try again")
        return redirect(request, "resend_verification_code")

    email = request.get('email')

    form = AccountConfirmationForm(request.POST or None)
    context = {
        'email': email,
        'form': form,
    }

    if form.is_valid():
        try:
            verification_code = form.cleaned_data.get('verification_code')
            ...submit to backend
        except Exception as e:
           ...handle exceptions
            messages.error(
                request,
                f'An error occurred trying to confirm your account'
            )
            return render(request, "confirm_account.html", context)
        else:
            return redirect(reverse('signup_success') + '?first_time=yes')

    return render(request, "confirm_account.html", context)

不幸的是,我的预期行为没有发生,尽管我承认这似乎不太正确。当从注册表单调用confirm_account_verification_code()时,django 是否真的使用视图函数?return render(request, 'confirm_account.html', context)我不完全确定我在这里做错了什么。

标签: pythondjangohttpauthenticationroutes

解决方案


推荐阅读