2
\$\begingroup\$

I intend to send validating message to User's email to complete a registration with the following codes:

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)

The problem is that the user/validate.html feedbacks to me very slowly after formulated form was submittd.
I guess it's send_email consuming lots of my patience.

How could I implement step 4 in advance which immediately show prompt message then send mail unhurried.

\$\endgroup\$
  • \$\begingroup\$ I don't understand... You don't send a response if the form is invalid or if the user send the GET verb. How do you display the form in the first place? How do you explain there were errors? \$\endgroup\$ – 301_Moved_Permanently Jun 2 '18 at 12:15