首先反馈提示页面,然后向用户发送验证电子邮件



我打算向用户的电子邮件发送验证消息,以使用以下代码完成注册:

def register(request):
"""1. register a new user
2. generate activating code
3. send validating email
4. prompt to check activating email.
"""
if request.method == "GET":
form = UserForm()
if request.method == "POST":
form = UserForm(request.POST)
print(vars(form))
if form.is_valid():
#1. Create User and save to sever
user = User.objects.create_user(
form.cleaned_data['username'],
first_name=form.cleaned_data['first_name'],
last_name=form.cleaned_data['last_name'],
email=form.cleaned_data['email'],
password=form.cleaned_data['password'])
user.is_active = False
user.save()
#2. Create activate code and save to server.
uuid_code = str(uuid.uuid4()).replace("-", '')
activate_code = ActivateCode(user=user, code=uuid_code)
activate_code.save()
#3. Send Validation Email
activate_link = "http://%s/user/activate/%s" %(request.get_host(), uuid_code)
activate_message = """
You're almost done!
<a href="%s">Click here to complete your registration</a>
""" % activate_link
send_mail(subject="Complete Registration With Code Journey",
message="Click here to complete your registration: %s" %activate_link,
html_message=activate_message,
from_email="anexmaple@mail.com",
recipient_list=[form.cleaned_data['email'],],
fail_silently=False)
#4. Prompt user to check his email.
context = {'email': form.cleaned_data['email'],}
return render(request, "user/validate.html", context)

问题是,在提交公式后,user/validate.html反馈给我的速度非常慢。
我想这send_email消耗了我很多耐心。

我如何提前实施步骤4,立即显示提示消息,然后不紧不慢地发送邮件。

这个问题可以通过定义一个新函数轻松解决;

#2. Prompt user to check his email.
#immediately after he submit the registration request.
context = {'email': form.cleaned_data['email'], }
def render_template(context):
return render(request, "user/validate.html", context)
render_template(context)

相关内容

最新更新