python - Django 渲染和提交从另一个表单传递给电子邮件验证码的表单
问题描述
我想完成以下操作,使用验证码进行电子邮件验证,而不是链接
- 用户在注册表单中注册
- 用户电子邮件作为上下文传递给验证码输入表单
- 用户收到验证码
- 用户在验证码输入表单中输入验证码
- 验证码在后端通过第一个表单上下文发送的电子邮件进行确认
所以我想要做的是,当用户访问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)
我不完全确定我在这里做错了什么。
解决方案
推荐阅读
- c# - 将 DateTimeOffset - UTC 转换为 LocalTime (CurrentCulture)
- node.js - 计时器到期时禁止或踢(Discord.js)
- firebase - Firestore,startAfter 和 startAfterDocument 的区别
- python - QuantLib 中 FixedRateBond 的 exCoupon 期限
- flutter - 第一次双击时未触发 onDoubleTap
- java - 将 ObservableList 绑定到 TreeTableView 项
- javascript - JS:找到下一个可用游戏的天数的有效算法
- .htaccess - 将所有 URls 重定向到文件夹内的文件,即使 URL 为空
- flutter - Flutter web 404在apache上没有哈希没有htaccess
- javascript - 缩小页面混乱对齐