亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

Django-simple-captcha驗(yàn)證碼包使用方法詳解

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

django-simple-captcha是django的驗(yàn)證碼包,非常簡單實(shí)用,這次記錄的是如何點(diǎn)擊驗(yàn)證碼后刷新驗(yàn)證碼,因?yàn)檫@個(gè)功能官方文檔并沒有詳細(xì)給出。

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.運(yùn)行 python manage.py makemigrations 和 python manage.py migrate

4.url路由加入urls.py的urlpatterns

urlpatterns = [ path(’captcha/’, include(’captcha.urls’)), # 圖片驗(yàn)證碼 路由 path(’refresh_captcha/’, views.refresh_captcha), # 刷新驗(yàn)證碼,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)建驗(yàn)證碼def captcha(): hashkey = CaptchaStore.generate_key() # 驗(yàn)證碼答案 image_url = captcha_image_url(hashkey) # 驗(yàn)證碼地址 captcha = {’hashkey’: hashkey, ’image_url’: image_url} return captcha#刷新驗(yàn)證碼def refresh_captcha(request): return HttpResponse(json.dumps(captcha()), content_type=’application/json’)# 驗(yàn)證驗(yàn)證碼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(): # 如果驗(yàn)證碼匹配return True except: return False else: return Falseclass IndexView(View): def get(self, request): hashkey = CaptchaStore.generate_key() # 驗(yàn)證碼答案 image_url = captcha_image_url(hashkey) # 驗(yàn)證碼地址 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) # 用戶提交的驗(yàn)證碼 key = request.POST.get('hashkey', None) # 驗(yàn)證碼答案 if jarge_captcha(capt, key): return HttpResponse('驗(yàn)證碼正確') else: return HttpResponse('驗(yàn)證碼錯誤')

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://www.aoyou183.cn/bcjs/17127.html#' rel='external nofollow' class='captcha'> <img src='http://www.aoyou183.cn/bcjs/{{ captcha.image_url }}' alt='點(diǎn)擊切換' > </a> <br> <input type='text' name='captcha' placeholder='驗(yàn)證碼'> <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)刷新驗(yàn)證碼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對驗(yàn)證碼進(jìn)行存儲,而是使用了數(shù)據(jù)庫,當(dāng)你在做數(shù)據(jù)庫遷移的時(shí)候會生成一個(gè)表 captcha_captchastore ,包含以下字段

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

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

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 国产免费不卡视频 | 91轻吻 | 三级欧美在线 | 久久国内精品自在自线400部o | 成人免费久久精品国产片久久影院 | 美国黄色a级片 | 免费啪视频观在线视频在线 | 拍拍拍美女黄色1000视频 | 欧美精品国产制服第一页 | 精品一区二区在线观看 | 26uuu欧美视频在线观看 | 亚洲精品日韩一区二区 | 免费国产在线观看不卡 | 午夜精品久久久久久 | 狠狠色丁香久久婷婷综合五月 | 久久青草免费91观看 | 日韩福利视频在线 | 三级成人影院 | 97精品在线视频 | 国产偷国产偷亚洲高清在线 | 66av视频| 午夜三级在线 | 日韩黄色在线观看 | 免费国产成人高清视频网站 | 一级生活片 | 久久青草91线频免费观看 | 99九九精品| 亚洲精品第一国产综合高清 | 97国产大学生情侣11在线视频 | 成年人黄色一级片 | 国产成人v爽在线免播放观看 | 欧美黄色片免费 | 国产孕妇做受视频在线观看 | 国产一区二区三区在线视频 | 男女交性高清全过程无遮挡 | 青青草这里只有精品 | 国产免费一区二区三区在线 | 久99久视频 | 女人被男人狂躁免费视频 | 国产成人v爽在线免播放观看 | 国内精品一区二区 |