django注冊用郵箱發送驗證碼的實現
lis = []#設置一個空列表用來存放發送的驗證碼,用來驗證def yzm1(): res1 = '' for i in range(4):#用四個隨機數組成驗證碼num = random.randint(0, 9)res1 += str(num) lis.append(res1)#將驗證碼放入空的列表中 return res1#返回驗證碼class zc(View): def get(self, request):return render(request, ’zc.html’, locals()) def post(self, request):name = request.POST[’qq’]#獲取前端傳遞進來的數據subject = ’驗證碼’message = vercode()#獲取驗證碼form = ’837620306@qq.com’#獲取發送驗證碼的郵箱# EMAIL_HOST_USERrcipient_list = [name]res = send_mail(subject=subject, message=message, from_email=form, recipient_list=rcipient_list)#發送驗證碼返貨真或假,假代表沒有發送成功if res == 1:#判斷是否發送成功 return redirect(’/app01/yzm/’)#成功的話跳轉到驗證的網頁else:#不成功的話返回注冊界面 return render(request, ’zc.html’, locals())def yzm2(request): if request.method == ’GET’:return render(request, ’yzm.html’, locals()) elif request.method == ’POST’:a = request.POST[’much’]#獲取到用戶輸入的驗證碼if a == lis[-1]:#取最新的驗證碼 return HttpResponse(’驗證碼正確’)else: return HttpResponse(’no’)
用來發送驗證碼的郵箱需要打開POP3/SMTP服務和IMAP/SMTP服務,并且獲得POP3/SMTP服務的授權碼
settings 配置文件另外需要對django的settings文件進行設置
# 設置發送郵件服務器:smtp.qq.com,EMAIL_HOST = ’smtp.qq.com’# 設置端口號,如果使用的是SSL,端口號為465或587EMAIL_PORT = 25#設置發件人郵箱EMAIL_HOST_USER = ’xxxxx’# 設置發件人 授權碼 (POP3/SMTP服務的授權碼)EMAIL_HOST_PASSWORD = ’xxxxx’# 設置是否啟用安全鏈接EMAIL_USER_TLS = True
到此這篇關于django注冊用郵箱發送驗證碼的實現的文章就介紹到這了,更多相關django 郵箱驗證碼內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
