這篇文章將為大家詳細講解有關如何正確的使用Django-simple-captcha驗證碼包,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
django-simple-captcha是django的驗證碼包,非常簡單實用,這次記錄的是如何點擊驗證碼后刷新驗證碼,因為這個功能官方文檔并沒有詳細給出。
1.安裝 pip install django-simple-captcha, pip install Pillow
2.將captcha 加入 settings.py 的 INSTALLED_APPS
3.運行 python manage.py makemigrations 和 python manage.py migrate
4.url路由加入urls.py的urlpatterns
urlpatterns = [ path('captcha/', include('captcha.urls')), # 圖片驗證碼 路由 path('refresh_captcha/', views.refresh_captcha), # 刷新驗證碼,ajax path('test/',IndexView.as_view()), #get與post請求路徑 ]
5.在views.py中加入以下代碼
from django.shortcuts import render from django.views.generic import View from captcha.models import CaptchaStore from captcha.helpers import captcha_image_url from django.http import HttpResponse import json # 創(chuàng)建驗證碼 def captcha(): hashkey = CaptchaStore.generate_key() # 驗證碼答案 image_url = captcha_image_url(hashkey) # 驗證碼地址 captcha = {'hashkey': hashkey, 'image_url': image_url} return captcha #刷新驗證碼 def refresh_captcha(request): return HttpResponse(json.dumps(captcha()), content_type='application/json') # 驗證驗證碼 def jarge_captcha(captchaStr, captchaHashkey): if captchaStr and captchaHashkey: try: # 獲取根據(jù)hashkey獲取數(shù)據(jù)庫中的response值 get_captcha = CaptchaStore.objects.get(hashkey=captchaHashkey) if get_captcha.response == captchaStr.lower(): # 如果驗證碼匹配 return True except: return False else: return False class IndexView(View): def get(self, request): hashkey = CaptchaStore.generate_key() # 驗證碼答案 image_url = captcha_image_url(hashkey) # 驗證碼地址 print(hashkey,image_url) captcha = {'hashkey': hashkey, 'image_url': image_url} return render(request, "login.html", locals()) def post(self, request): capt = request.POST.get("captcha", None) # 用戶提交的驗證碼 key = request.POST.get("hashkey", None) # 驗證碼答案 if jarge_captcha(capt, key): return HttpResponse("驗證碼正確") else: return HttpResponse("驗證碼錯誤")
6.templates文件夾下login.html的內(nèi)容
{% load static %} <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="/tupian/20230522/script> <script src="/tupian/20230522/script> </head> <body> <form action="/test/" method="post"> {% csrf_token %} <a href="#" rel="external nofollow" class="captcha"> <img src="{{ captcha.image_url }}" alt="點擊切換" id="id_captcha" > </a> <br> <input type="text" name="captcha" placeholder="驗證碼"> <br> <input value="{{ captcha.hashkey }}" name="hashkey" type="hidden" id="id_captcha_0"> <button type="submit" class="btn btn-primary btn-block ">提交</button> </form> <script> <!-- 動態(tài)刷新驗證碼js --> $(document).ready(function(){ $('.captcha').click(function () { $.getJSON("/refresh_captcha/", function (result) { $('#id_captcha').attr('src', result['image_url']); $('#id_captcha_0').val(result['hashkey']) }); }); }); </script> </body> </html>
django-simple-captcha并沒有使用session對驗證碼進行存儲,而是使用了數(shù)據(jù)庫,當你在做數(shù)據(jù)庫遷移的時候會生成一個表 captcha_captchastore ,包含以下字段
challenge = models.CharField(blank=False, max_length=32) # 驗證碼大寫或者數(shù)學計算比如 1+1
response = models.CharField(blank=False, max_length=32) # 需要輸入的驗證碼 驗證碼小寫或數(shù)學計算的結果 比如 2
hashkey = models.CharField(blank=False, max_length=40, unique=True) # hash值
expiration = models.DateTimeField(blank=False) # 到期時間
關于如何正確的使用Django-simple-captcha驗證碼包就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當前名稱:如何正確的使用Django-simple-captcha驗證碼包-創(chuàng)新互聯(lián)
當前鏈接:http://www.rwnh.cn/article46/jogeg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、服務器托管、做網(wǎng)站、域名注冊、網(wǎng)站導航、外貿(mào)網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)