国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Django-simple-captcha驗證碼包使用方法詳解

瀏覽:32日期:2024-09-16 16:30:49

django-simple-captcha是django的驗證碼包,非常簡單實用,這次記錄的是如何點擊驗證碼后刷新驗證碼,因為這個功能官方文檔并沒有詳細給出。

django-simple-captcha官方文檔:http://django-simple-captcha.readthedocs.io/en/latest/

django-simple-captcha的github網(wǎng)址:https://github.com/mbi/django-simple-captcha

開始

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 renderfrom django.views.generic import Viewfrom captcha.models import CaptchaStorefrom captcha.helpers import captcha_image_urlfrom django.http import HttpResponseimport 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 Falseclass 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='https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js'></script> <script src='https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.js'></script></head><body> <form action='/test/' method='post'> {% csrf_token %} <a href='http://m.cgvv.com.cn/bcjs/17127.html#' rel='external nofollow' class='captcha'> <img src='http://m.cgvv.com.cn/bcjs/{{ captcha.image_url }}' alt='點擊切換' > </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ù)庫,當(dāng)你在做數(shù)據(jù)庫遷移的時候會生成一個表 captcha_captchastore ,包含以下字段

challenge = models.CharField(blank=False, max_length=32) # 驗證碼大寫或者數(shù)學(xué)計算比如 1+1response = models.CharField(blank=False, max_length=32) # 需要輸入的驗證碼 驗證碼小寫或數(shù)學(xué)計算的結(jié)果 比如 2hashkey = models.CharField(blank=False, max_length=40, unique=True) # hash值expiration = models.DateTimeField(blank=False) # 到期時間

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 欧美做爰野外在线视频观看 | 女人扒开双腿让男人捅 | 欧美成人在线免费观看 | 日本久久久 | 欧美日韩一区二区不卡三区 | 中文字幕在线乱码不卡区区 | 国产日韩在线观看视频 | 亚洲第一网站在线观看 | 国产精品日产三级在线观看 | 亚洲清纯自偷自拍另类专区 | 国产免费久久精品99 | 一本色道久久88亚洲精品综合 | 玖玖影院在线观看 | 夜色邦合成福利网站 | 天堂在线视频网站 | 日本一区二区高清免费不卡 | 日本久久一区二区 | 91亚洲国产成人久久精品网站 | 91亚洲精品国产第一区 | 日本道在线播放 | 男女扒开双腿猛进入免费网站 | 特级深夜a级毛片免费观看 特级生活片 | 亚洲欧美视频在线观看 | 在线视频观看国产 | 综合网自拍 | 夜色毛片永久免费 | 欧美高清正版在线 | 亚洲国产高清一区二区三区 | 日韩特级 | 欧美三级久久 | 99视频精品| 手机日韩理论片在线播放 | 三级黄色毛片网站 | 午夜两性视频 | 国产精品aⅴ | 精品国产一区二区 | a免费视频| 欧美毛片a级毛片免费观 | 久久久久国产精品免费网站 | 免费在线视频成人 | 欧美日韩亚洲第一页 |