Python编程django实现同一个ip十分钟内只能注册一次
作者:北漂的雷子 时间:2023-01-18 17:01:55
很多小伙伴都会有这样的问题,说一个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/p/7774768.html
标签:python,django
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaScript转换与解析JSON方法实例详解
2024-04-17 10:22:44
ASP + XML + JavaScript 实现动态无限级联动菜单
2008-06-13 06:31:00
js如何读取csv内容拼接成json
2023-08-13 02:20:14
![](https://img.aspxhome.com/file/2023/4/56034_0s.png)
python 使用plt画图,去除图片四周的白边方法
2022-02-07 19:34:41
![](https://img.aspxhome.com/file/2023/3/100783_0s.jpg)
python爬虫之基金信息存储
2021-12-25 03:20:04
![](https://img.aspxhome.com/file/2023/8/134518_0s.webp)
如何用OleDbDataAdapter来对数据库进行操作?
2010-06-12 12:56:00
python抓取最新博客内容并生成Rss
2022-06-18 08:38:40
JavaScript遍历求解数独问题的主要思路小结
2023-10-13 16:41:14
python生成器/yield协程/gevent写简单的图片下载器功能示例
2022-02-25 10:08:03
Python自动化办公之Excel拆分与自动发邮件
2022-05-03 09:05:40
![](https://img.aspxhome.com/file/2023/4/93464_0s.png)
Python叠加矩形框图层2种方法及效果
2022-10-09 15:59:53
![](https://img.aspxhome.com/file/2023/6/105606_0s.jpg)
sql存储过程的使用和介绍
2024-01-14 01:58:19
Python读取图片EXIF信息类库介绍和使用实例
2023-05-27 00:49:47
PHP中DirectIO操作文件扩展的用法大全
2023-06-13 13:58:45
MySQL中的联合索引学习教程
2024-01-21 23:49:34
MySQL索引优化实例分析
2024-01-26 18:34:27
![](https://img.aspxhome.com/file/2023/5/112675_0s.webp)
Python中优化NumPy包使用性能的教程
2021-03-27 21:08:43
asp如何编写一个最简单的聊天程序?
2009-11-08 19:04:00
详解Go语言中泛型的实现原理与使用
2024-02-06 08:30:15
Python yield生成器和return对比代码实例
2022-07-17 21:54:57