0

我有一个用户注册表格。用户按下提交按钮后,用户应该会收到一封确认电子邮件。整个表格将发布到数据库。但问题是我使用Gmail作为域电子邮件,它会导致很多安全预防,当有新注册时我无法发送确认电子邮件。因此,系统无法将表单发布到数据库。用户一直停留在#loading页面中。

现在,我将忽略确认电子邮件过程。当有新的注册时,Gmail 系统仍然会发送一封确认邮件。 但是无论确认电子邮件是否成功发送,表格都会发布。 如何修改代码?

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();
                    }
    
            }
        });
    }

视图.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')
4

0 回答 0