我打算向用户的电子邮件发送验证邮件,以使用以下代码完成注册:
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步,立即显示提示信息,然后发送邮件不紧不慢。
答案 0 :(得分:0)
通过定义一个新函数可以很容易地解决这个问题;
#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)