0

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.

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • 2
    Possible duplicate of [How To Perform asynchronous task in django?](https://stackoverflow.com/questions/44433721/how-to-perform-asynchronous-task-in-django) – solarissmoke Jun 02 '18 at 08:46
  • asynchronous is truly hard to wrap my head around. Could you please post an answer to aid the learning. @solarissmoke – AbstProcDo Jun 02 '18 at 08:54
  • I would just be repeating the answers that others have already written, which is not the purpose of Stack Overflow. The comments and answers on the question I linked to provide links to the relevant resources. – solarissmoke Jun 02 '18 at 09:04

1 Answers1

0

This problem could be easily solved by define a new function;

        #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)
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138