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版本的登录注册已经实现完毕。

  • 相关阅读:
    The type new View.OnClickListener(){} must implement the inherited abstract method View.Onclicklis
    vue开发环境跨域
    浅析deep深度选择器
    模块化
    highlight-current-row无效的解决方法
    element-ui的table 在页面缩放时,出现的问题
    css变量
    节流和防抖
    promise详解
    正则表达式详解
  • 原文地址:https://www.cnblogs.com/leiziv5/p/7792179.html
Copyright © 2011-2022 走看看