zoukankan      html  css  js  c++  java
  • django 实现同一个ip十分钟内只能注册一次(redis版本)

        上一篇文章,django 实现同一个ip十分钟内只能注册一次 的时候,我们在注册的时候选择使用的使我们的数据库来报错我们的注册的ip信息,可是如果数据量大,用户多的时候,单单靠我们的数据库

    来储存我们的注册的ip地址信息。   而使用redis来存储的话,这样我们就少了一张表,少了数据库查询,你要是开发经验多了,就明白这里少用数据库查询的好处了,会减少很大的数据库压力。

     那么我们现在来试着去构思我们的注册的内容,思路:

        注册,用户post数据》取到ip去redis去判断,存在》十分钟内不能注册提示

       注册,用户post数据》取到ip去redis去判断,不存在》注册,在redis中去存储这个ip信息过期时间为600s

      

        接下来就是去实现我们的代码了。

       

    import redis
    r=redis.Redis(host='127.0.0.1',port=6379,db=0)
    class RegView(View):
        def get(self,request):
            return  render(request,'reg.html')
        def post(self,request):
            ipreques = request.META['REMOTE_ADDR']
            ip_re = r.get(ipreques)
            if ip_re:
                return render(request, 'reg.html', {'msg': u'10分钟只能注册一次'})
            username=request.POST['username']
            if len(getuser(username))<=0:
                return render(request,'reg.html',{'msg':u'用户名应该是6-16组成'})
            passwor1 = request.POST['password']
            passwor2 = request.POST['password1']
            shouj = request.POST['shouji']
            if len(getPhoneNumFromFile(shouj))<=0:
                return render(request, 'reg.html', {'msg':u'手机号格式是否正确'})
            shouji = User.objects.filter(mobile__exact=shouj)
            if shouji:
                return render(request, 'reg.html', {'msg': u'手机号已经存在'})
            youjian = request.POST['email']
            if len(getMailAddFromFile(youjian))<=0:
                return render(request, 'reg.html', {'msg': u'邮箱格式是否正确'})
            use=User.objects.filter(username__exact=username)
            if use:
                return render(request,'reg.html',{'msg':u'用户名已经存在'})
            else:
                if passwor1==passwor2:
                    use1=User()
                    use1.username=username
                    use1.password=make_password(passwor1)
                    use1.mobile=shouj
                    use1.email=youjian
                    use1.save()
                    r.set(ipreques,1,ex=600)
                    return HttpResponseRedirect('login')
                else:
                    return render(request,'reg.html',{'msg':u'请查看密码是否一致'})
    

       代码实现后,我们需要进行去验证, 首次注册成功,

       我们来看看redis存没有

      再次注册

        这样我们的redis版本的登录注册已经实现完毕。

  • 相关阅读:
    Delphi 10.3.3解决Android 11闪退
    QuickCore
    Delphi 10.4.1使用传统代码提示方案
    LINUX SHELL条件判断
    C#程序集使用强名字(Strong Name)签名/强名称签名
    ASP.NET Core环境变量和启动设置的配置教程
    ASP.NET Core MVC获取请求的参数方法示例
    Fluentvalidation的基本使用
    netstat & crontab
    Linux/Centos下多种方法查看系统block size大小
  • 原文地址:https://www.cnblogs.com/leiziv5/p/7792179.html
Copyright © 2011-2022 走看看