使用Django實現(xiàn)商城驗證碼模塊的方法
本文主要涉及圖形驗證碼的相關(guān)功能,主要包括,圖形驗證碼獲取、驗證碼文字存儲、驗證碼生成等。
圖形驗證碼接口設(shè)計和定義
驗證碼獲取接口設(shè)計
uuid作為路徑參數(shù),唯一標(biāo)識驗證碼所屬用戶
新建應(yīng)用
驗證碼的相關(guān)邏輯我們用一個單獨的app處理,所以這里需要新建一個叫verifications的app,建好app后,打開views.py視圖文件,編寫一個驗證碼的視圖類
class ImageCodeView(View): """圖形驗證碼""" def get(self, request, uuid): """ :param request: 請求對象 :param uuid: 唯一標(biāo)識圖形驗證碼所屬于的用戶 :return: image/jpg """ pass
然后配置路由
項目路由配置:
path('', include('apps.verifications.urls')),
配置app的路由
path('image_codes/``uuid:uuid``/', views.ImageCodeView.as_view()),
驗證碼處理相關(guān)準(zhǔn)備工作
準(zhǔn)備captcha擴展包
把captcha擴展包放到verifications的lib目錄下,然后需要安裝Python的圖片處理庫,pip install Pillow
準(zhǔn)備Redis數(shù)據(jù)庫
redis用來存儲圖片驗證碼上的數(shù)字,后面會用來做校驗
"verify_code": { # 驗證碼 "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/2", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } },
圖形驗證碼后端邏輯實現(xiàn)
class ImageCodeView(View): """圖形驗證碼 """ def get(self, request, uuid): """ 實現(xiàn)圖形驗證碼邏輯 :param uuid: UUID :return: image/jpg """ # 生成圖形驗證碼 text, image = captcha.generate_captcha() # 保存圖形驗證碼 # 使用配置的redis數(shù)據(jù)庫的別名,創(chuàng)建連接到redis的對象 redis_conn = get_redis_connection('verify_code') # 使用連接到redis的對象去操作數(shù)據(jù)存儲到redis # redis_conn.set('key', 'value') # 因為沒有有效期 # 圖形驗證碼必須要有有效期的:設(shè)計是300秒有效期 # redis_conn.setex('key', '過期時間', 'value') redis_conn.setex('img_%s' % uuid, 300, text) # 響應(yīng)圖形驗證碼: image/jpg return http.HttpResponse(image, content_type='image/jpg')
圖形驗證碼前端邏輯
Vue實現(xiàn)圖形驗證碼展示
1.register.js
mounted(){ // 生成圖形驗證碼 this.generate_image_code(); }, methods: { // 生成圖形驗證碼 generate_image_code(){ // 生成UUID。generateUUID() : 封裝在common.js文件中,需要提前引入 this.uuid = generateUUID(); // 拼接圖形驗證碼請求地址 this.image_code_url = "/image_codes/" + this.uuid + "/"; }, ...... }
2.register.html
<li> <label>圖形驗證碼:</label> <input type="text" name="image_code" id="pic_code" class="msg_input"> <img :src="image_code_url" @click="generate_image_code" alt="圖形驗證碼" class="pic_code"> <span class="error_tip">請?zhí)顚憟D形驗證碼</span> </li>
3.圖形驗證碼展示和存儲效果
Vue實現(xiàn)圖形驗證碼校驗
1.register.html
<li> <label>圖形驗證碼:</label> <input type="text" v-model="image_code" @blur="check_image_code" name="image_code" id="pic_code" class="msg_input"> <img :src="image_code_url" @click="generate_image_code" alt="圖形驗證碼" class="pic_code"> <span class="error_tip" v-show="error_image_code">[[ error_image_code_message ]]</span> </li>
2.register.js
check_image_code(){ if(!this.image_code) { this.error_image_code_message = '請?zhí)顚憟D片驗證碼'; this.error_image_code = true; } else { this.error_image_code = false; } },
3.圖形驗證碼校驗效果
至此驗證碼部分就說完了
到此這篇關(guān)于使用Django實現(xiàn)商城驗證碼模塊的方法的文章就介紹到這了,更多相關(guān)Django 商城驗證碼模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python基于similarities實現(xiàn)文本語義相似度計算和文本匹配搜索
similarities?實現(xiàn)了多種相似度計算、匹配搜索算法,支持文本、圖像,python3開發(fā),下面我們就來看看如何使用similarities實現(xiàn)文本語義相似度計算和文本匹配搜索吧2024-03-03探索Python數(shù)據(jù)可視化庫中Plotly Express的使用方法
在數(shù)據(jù)分析和可視化領(lǐng)域,數(shù)據(jù)的有效呈現(xiàn)是至關(guān)重要的,python作為一種強大的編程語言,提供了多種數(shù)據(jù)可視化工具和庫,本文將介紹Plotly Express的基本概念和使用方法,幫助讀者快速入門并掌握數(shù)據(jù)可視化的技巧2023-06-06Python import與from import使用和區(qū)別解讀
Python程序可以調(diào)用一組基本的函數(shù)(即內(nèi)建函數(shù)),比如print()、input()和len()等函數(shù)。接下來通過本文給大家介紹Python import與from import使用及區(qū)別介紹,感興趣的朋友一起看看吧2021-09-09