首页 > 其他 > 详细

图片验证码接口

时间:2020-10-06 21:56:05      阅读:33      评论:0      收藏:0      [点我收藏+]

1,1   Django 缓存设置

  django的六种缓存(mysql+redis) :https://www.cnblogs.com/xiaonq/p/7978402.html#i6

1,2  安装Django缓存模块

  pip install django-redis==4.12.1

1,3 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使用redis存储

  SESSION_CACHE_ALIAS = "session"

  #配置session存储的位置 : 使用cache中的 session配置

2, 新建应用verifications

  1,图形验证码

  2,短信验证码

  3,邮件验证

       在apps文件夹下新建应用:  verifications

  python ../manage.py startapp verifications

     在 syl 下的settings里面配置应用

  INSTALLED_APPS = [

       ‘verifications.apps.VerificationsConfig‘,

            ]

   在urls中添加路由

    path(‘verify‘, include(‘verifications.urls‘)),

    添加子路由

    from django.urls import path

    from . import views

    urlpatterns = [  

      # path(‘image_codes/‘, views.ImageCodeView.as_view())

         ]

3,图形验证码captcha使用  

  1,下载captcha压缩包captcha.zip 放到项目packages文件夹下 

  2,解压captcha.zip放到syl/libs 文件夹下

  3,解压文件中的syl/libs/ccaptcha/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客户端                # 5.写入redis(是字符串)              redis_client.setex(uuid, 60 * 5, text)
            return HttpResponse(image, content_type=‘image/jpg‘)

 

技术分享图片

    

    

        

图片验证码接口

原文:https://www.cnblogs.com/wwzy/p/13774228.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!