最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年11月28日 15:41:27   作者:-零  
這篇文章主要介紹了Django-simple-captcha驗證碼包使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

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 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的內容

{% 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="#" 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) # 到期時間

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Python?tkinter?列表框Listbox屬性詳情

    Python?tkinter?列表框Listbox屬性詳情

    這篇文章主要介紹了Python?tkinter?列表框Listbox屬性詳情,列表框控件顯示多行文本,用戶可以選中一行或者多行。所有的文本只能使用一種字體,不能混合使用多種字體
    2022-07-07
  • python包和文件夾有的區(qū)別點總結

    python包和文件夾有的區(qū)別點總結

    在本篇文章里小編給大家整理的是一篇關于python包和文件夾有的區(qū)別點總結內容,有興趣的朋友們可以學習參考下。
    2021-09-09
  • 簡單了解python關鍵字global nonlocal區(qū)別

    簡單了解python關鍵字global nonlocal區(qū)別

    這篇文章主要介紹了簡單了解python關鍵字global nonlocal區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • python數(shù)據(jù)結構leetcode338比特位計數(shù)算法

    python數(shù)據(jù)結構leetcode338比特位計數(shù)算法

    這篇文章主要介紹了力扣刷題中python數(shù)據(jù)結構leetcode338比特位計數(shù)算法解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • 詳解如何列出已安裝的Python包

    詳解如何列出已安裝的Python包

    處理 Python 項目可能需要列出已安裝的 Python 包,以便管理依賴項、檢查更新或與其他人共享項目需求,在這篇文章中,我們將研究多種用于列出系統(tǒng)上安裝的 Python 包的技術
    2023-10-10
  • PyQt5之基礎框架解讀

    PyQt5之基礎框架解讀

    這篇文章主要介紹了PyQt5之基礎框架,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python清除字符串里非數(shù)字字符的方法

    python清除字符串里非數(shù)字字符的方法

    這篇文章主要介紹了python清除字符串里非數(shù)字字符的方法,涉及Python使用re模塊正則替換操作字符串的技巧,需要的朋友可以參考下
    2015-07-07
  • Pandas之DataFrame對象的列和索引之間的轉化

    Pandas之DataFrame對象的列和索引之間的轉化

    這篇文章主要介紹了Pandas之DataFrame對象的列和索引之間的轉化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • Python中函數(shù)參數(shù)調用方式分析

    Python中函數(shù)參數(shù)調用方式分析

    這篇文章主要介紹了Python中函數(shù)參數(shù)調用方式,結合實例形式分析了Python函數(shù)參數(shù)定義與使用的四種常見操作方法,需要的朋友可以參考下
    2018-08-08
  • python多環(huán)境切換及pyenv使用過程詳解

    python多環(huán)境切換及pyenv使用過程詳解

    這篇文章主要介紹了python多環(huán)境切換及pyenv使用過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09

最新評論

临泉县| 怀来县| 万宁市| 黔江区| 瑞昌市| 兰坪| 景德镇市| 北辰区| 长武县| 襄樊市| 云浮市| 和龙市| 江川县| 栖霞市| 九江县| 颍上县| 邢台市| 鸡泽县| 苍南县| 谢通门县| 南通市| 万源市| 仲巴县| 新余市| 大埔县| 大丰市| 准格尔旗| 夏河县| 彭山县| 开化县| 延边| 蒲城县| 大城县| 定安县| 聂荣县| 新巴尔虎右旗| 临高县| 葵青区| 舒城县| 彭山县| 阿瓦提县|