1. 程式人生 > 實用技巧 >DOM節點型別

DOM節點型別

驗證碼介面

  1. 安裝模組

    • pip install django-redis==4.12.1
      
  2. syl/settings.py 中配置快取

    • .django 快取設定

    • # 快取配置
      CACHES = {
          # django存緩預設位置,redis 0號庫
          # default: 連線名稱
          "default": {
              "BACKEND": "django_redis.cache.RedisCache",
              "LOCATION": "redis://127.0.0.1:6379/0",
              "OPTIONS": {
                  "CLIENT_CLASS": "django_redis.client.DefaultClient",
              }
          },
          # django session存 reidis 1 號庫(現在基本不需要使用)
          "session": {
              "BACKEND": "django_redis.cache.RedisCache",
              "LOCATION": "redis://127.0.0.1:6379/1",
              "OPTIONS": {
                  "CLIENT_CLASS": "django_redis.client.DefaultClient",
              }
          },
          # 圖形驗證碼,存redis 2號庫
          "img_code": {
              "BACKEND": "django_redis.cache.RedisCache",
              "LOCATION": "redis://127.0.0.1:6379/2",
              "OPTIONS": {
                  "CLIENT_CLASS": "django_redis.client.DefaultClient",
              }
          }
      }
      # 配置session使用redis儲存
      SESSION_ENGINE = "django.contrib.sessions.backends.cache"
      # 配置session儲存的位置: 使用cache中的 session配置
      SESSION_CACHE_ALIAS = "session"
      
      
  3. 新建應用verifications

    • 圖形驗證碼

    • 簡訊驗證碼

    • 郵件驗證

    • '''2.1 在apps資料夾下新建應用: verifications'''
      python ../manage.py startapp verifications # 切換到apps資料夾下執行建立命令
      
    • '''2.2 在syl/settings.py中新增應用'''
      INSTALLED_APPS = [
      		'verifications.apps.VerificationsConfig',
      ]
      
    • '''2.3 在syl/urls.py主路由中新增'''
      path('verify/', include('verifications.urls')),
      
    • '''2.4 新增子路由: verifications/urls.py'''
      from django.urls import path
      from . import views
      
      urlpatterns = [
      	path('image_codes/', views.ImageCodeView.as_view())
      ]
      
      
  4. 圖形驗證碼captcha使用

    • 1.下載captcha壓縮包captcha.zip,放到專案packages資料夾下
      2.解壓captcha.zip放到syl/libs資料夾下
      3.解壓檔案中的syl/libs/captcha/captcha.py 右鍵執行即可生成圖片驗證碼
      unzip xxx.zip
      
  5. 在verifications/views.py中使用

    • from django.shortcuts import render
      
      # Create your views here.
      from django.http import HttpResponse, HttpResponseForbidden
      from django.views import View
      from django_redis import get_redis_connection
      from libs.captcha.captcha import captcha
      
      
      class ImageCodeView(View):
          def get(self, request):
              # 1.接收資料
              uuid = request.GET.get('uuid')
              # 2.校驗資料
              if not uuid:
                  return HttpResponseForbidden('uuid無效')
              # 3.處理業務
              # 獲取圖片文字內容和圖片二進位制程式碼
              text, image = captcha.generate_captcha()
              # 4.把uuid和圖片文字存入redis
              redis_client = get_redis_connection('img_code')  # 獲取redis客戶端
              # 5.寫入redis(是字串)
              redis_client.setex(uuid, 60 * 5, text)
              # 6.返回響應圖片
              return HttpResponse(image, content_type='image/jpg')
      
  6. 測試驗證碼介面

    • http://192.168.56.100:8888/verify/image_codes/?uuid=66ea64aa-fbe6-11ea-a3d3-005056c00008
      

  7. redis 檢視

    • 127.0.0.1:6379># select 2
      OK
      
      
      127.0.0.1:6379[2]># keys *
      1) "66ea64aa-fbe6-11ea-a3d3-005056c00008"
      127.0.0.1:6379[2]># get 66ea64aa-fbe6-11ea-a3d3-005056c00008
      "JEZ6"