django實現同一個ip十分鐘內只能註冊一次的執行個體,django十分鐘

來源:互聯網
上載者:User

django實現同一個ip十分鐘內只能註冊一次的執行個體,django十分鐘

很多小夥伴都會有這樣的問題,說一個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 Viewfrom blog.models import Ipclass 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一段時間的註冊的次數。

以上這篇django實現同一個ip十分鐘內只能註冊一次的執行個體就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.