How to ignore the HTTP response and post the form?

129 Views Asked by At

I have a form for user registration .After the users press the submit button , the user should receive a confirmation email .And the whole form will post to the db . But the problem is I use Gmail as the domain email and it causes lots of security prevention that I can't send the confirmation email when there is new registration. Therefore , the system can't post the form to the db . The user keep stucking in the #loading page.

Now , I'm going to ignore the confirmation email process . Gmail system still send a confirmation email when there is a new registration . But the form will then post anyway, no matter the confirmation email successfully sends it out or not. How to amend the code ?

HTML

var form = $("#form_data")[0];

    var data_item = new FormData(form);
    data_item.append("profile_img",document.getElementById("imgInp").files[0])
    data_item.append("username",username)
    data_item.append("password",password)
.....
    $("#loading").show()
    $.ajax({
                method : "POST",
                url : "/signup/",
                enctype : "mutipart/form_data",
                processData : false,
                contentType : false,
                cache : false,
                data : data_item,
                success : function(response){
                    console.log(response)
                    $("#loading").hide()
                    if (response == "success")
                    swal("Registration Saved Successfully", {
                                    icon: "success",
                                    button: "Ok",
                                    closeOnClickOutside: false,     
                                    }).then(function() {
                                    location.href = "/signin/";
                                    });
                            
                                
                    else if (response == "email exists"){
                        $('#error_email_exists').show();
                        $('#email').focus();    
                    }
                
                    else if (response == "user_name_exists"){
                        $('#error_name_exists').show();
                        $('#username').focus();
                    }
    
            }
        });
    }

View.py

def signup(request):
    username=request.POST.get('username')
    email=request.POST.get('email')
    password=request.POST.get('password')
...
    
    
    if email_all=="false":
        email_all=False
    else:
        email_all=True
    
    if phone_all=="false":
        phone_all=False
    else:
        phone_all=True
        
    if Signup.objects.filter(username=username).exists():
        return HttpResponse("user_name_exists")
    elif Signup.objects.filter(email=email).exists():
        return HttpResponse("email exists")
        
    else:
        
        if dob:
            user_obj=Signup(username=username,email=email,password=password,phoneno=phone_no,dob=dob,gender=gender,profile_image=profile_img,email_status=email_all,pwd_status=phone_all,bio=bio)
            user_obj.save()
        else:
            user_obj=Signup(username=username,email=email,password=password,phoneno=phone_no,gender=gender,profile_image=profile_img,email_status=email_all,pwd_status=phone_all,bio=bio)
            user_obj.save()
        
        
        subject = "....";
        
        html_message = render_to_string('email_body_signup.html', {'context': 'values'})
        
        plain_message  = strip_tags(html_message)
        
        email_from = settings.EMAIL_HOST_USER
        
        if mail.send_mail(subject, plain_message, email_from, [email],html_message = html_message):
            
            return HttpResponse("success")
        
        else:
            pass
        
        return HttpResponse("success")

def register(request):
    return render(request,'register.html')
0

There are 0 best solutions below