ALTER TABLE ADD COLUMN遇到DRM報錯
阿新 • • 發佈:2020-11-02
1安裝Django快取模組
pip install django-redis==4.12.1
2syl/settings.py中配置快取
# 快取配置 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", "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
在apps檔案下新建應用: verifications #切換到apps檔案下執行建立命令 python ../manage.py startapp verifications 2 在syl/settings.py中新增應用 INSTALLED_APPS=[ 'verifications.apps.VerificationsConfig' ] 3 在syl/urls.py 主路由中新增 path('verify/', include('verifications.urls')), 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.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')