1.django 缓存设置
django的六种缓存(mysql+redis) :https://www.cnblogs.com/xiaonq/p/7978402.html#i6
1.1 安装Django缓存模块
pip install django-redis==4.12.1
1.2 syl/settings.py 中配置缓存
CACHES
= {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0",
"OPTIONS": {
"CLIENT_CLASS":"django_redis.client.DefaultClient",
}
},
"session": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS":"django_redis.client.DefaultClient",
}
},
"img_code": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/2",
"OPTIONS": {
"CLIENT_CLASS":"django_redis.client.DefaultClient",
}
}
}
SESSION_ENGINE
= "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS
= "session"
2.新建应用verifications
图形验证码短信验证码邮件验证
'''2.1 在apps文件夹下新建应用: verifications'''
python
../manage
.py startapp verifications
'''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
= [
]
3.图形验证码captcha使用
1.下载captcha压缩包captcha
.zip,放到项目packages文件夹下
2.解压captcha
.zip放到syl
/libs文件夹下
3.解压文件中的syl
/libs
/captcha
/captcha
.py 右键运行即可生成图
4.在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
):
uuid
= request
.GET
.get
('uuid')
if not uuid
: return HttpResponseForbidden
('uuid无效')
text
, image
= captcha
.generate_captcha
()
redis_client
= get_redis_connection
('img_code')
redis_client
.setex
(uuid
, 60 * 5, text
)
return HttpResponse
(image
, content_type
='image/jpg')
5.测试验证码接口
`http
://192.168.56.100:8888/verify
/image_codes
/?uuid
=66ea64aa-fbe6
-11ea-a3d3
- 005056c00008`
127.0.0.1:6379>
OK
127.0.0.1:6379[2]>
1) "66ea64aa-fbe6-11ea-a3d3-005056c00008"
127.0.0.1:6379[2]>
"JEZ6"