django实现同一个ip十分钟内只能注册一次的实例
作者:北漂的雷子 时间:2021-03-07 03:13:37
很多小伙伴都会有这样的问题,说一个ip地址十分钟内之内注册一次,用来防止用户来重复注册带来不必要的麻烦
逻辑:
取ip,在数据库找ip是否存在,存在判断当前时间和ip上次访问时间之差,小于600不能注册,到登录界面,大于600可以注册,
设计一个数据库来存储这个ip地址和访问时间,
class Ip(models.Model):
ip=models.CharField(max_length=20)
time=models.DateTimeField()
class Meta:
verbose_name = u'访问时间'
verbose_name_plural = verbose_name
def __str__(self):
return self.ip
然后去
python manage.py makemigrations
python manage.py migrate
这样来更新我们的数据库,然后我们运行我们的项目可以在后台看到我们新注册的ip的数据
我们根据前面的逻辑,可以来设计我们的代码,
from django.views.generic.base import View
from blog.models import Ip
class RegView(View):
def get(self,request):
ipreques = request.META['REMOTE_ADDR']
try:
ip_c=Ip.objects.get(ip=ipreques)
if ip_c :
if (datetime.datetime.now()-ip_c.time).total_seconds()<600:
return render(request, 'login.html', {'msg': u'10分钟内只能注册一次'})
ip_c.time=datetime.datetime.now()
ip_c.save()
return render(request, 'reg.html')
except Exception as e:
new=Ip()
new.ip=str(ipreques)
new.time=datetime.datetime.now()
new.save()
return render(request, 'reg.html')
def post(self,request):
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=passwor1
use1.mobile=shouj
use1.email=youjian
use1.save()
return HttpResponseRedirect('login')
else:
return render(request,'reg.html',{'msg':u'请查看密码是否一致'})
return render(request,'reg.html')
其实这样,我们的整个过程就已经构建完毕,代码出来后,有小伙伴会问,你这代码怎么和我用的不一样,
我们都是函数式编程,其实很简单,我们去集成View类就可以实现我们的面向对象的编程,在url中我们只需要这么来写我们的代码。
url(r'^reg$', RegView.as_view(),name='reg'),
这样我们就可以完成了限制同个ip一段时间的注册的次数。
来源:http://www.cnblogs.com/leiziv5/archive/2017/11/02/7774768.html
标签:django,ip,十分钟,注册一次
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python打造爬虫代理池过程解析
2021-10-14 23:43:44
CSS实现DIV完美垂直居中(支持多浏览器)
2007-08-13 09:21:00
Tensorflow的DataSet的使用详解
2021-03-19 18:18:04
python3图片转换二进制存入mysql
2023-05-18 07:06:50
Python基于xlrd模块处理合并单元格
2023-08-28 21:10:49
![](https://img.aspxhome.com/file/2023/9/83959_0s.png)
Python中Yield的基本用法
2021-08-30 15:34:55
小技巧解决“FF不能读取outerHTML”的问题
2009-02-10 10:44:00
Request.Servervariables(“HTTP_USER_AGENT“)是什么意思。
2009-08-21 13:13:00
关于分页
2011-07-01 12:39:03
jQuery 让人恋恋不舍的秘密
2010-01-20 10:09:00
Python OpenCV去除字母后面的杂线操作
2023-08-02 15:18:47
![](https://img.aspxhome.com/file/2023/7/60947_0s.jpg)
怎么检测到点击图片的左右位置 像QQ空间那样
2008-10-04 10:45:00
驯服不听话的网页表格
2007-12-03 11:36:00
python 3.7.0 安装配置方法图文教程
2023-10-14 22:24:28
![](https://img.aspxhome.com/file/2023/2/65522_0s.jpg)
pyinstaller打包多个py文件和去除cmd黑框的方法
2022-12-18 13:52:30
![](https://img.aspxhome.com/file/2023/9/80739_0s.jpg)
Python实现双向链表基本操作
2022-07-22 10:14:12
![](https://img.aspxhome.com/file/2023/4/99214_0s.jpg)
python 利用turtle库绘制笑脸和哭脸的例子
2022-01-16 08:35:30
![](https://img.aspxhome.com/file/2023/7/86157_0s.jpg)
dataframe 按条件替换某一列中的值方法
2021-06-16 00:23:33
asp代码WinHttp.WinHttpRequest.5.1使用例子
2010-03-11 21:28:00
python模拟菜刀反弹shell绕过限制【推荐】
2023-07-13 12:56:18