Python programming django implements the same ip address can only be registered once in 10 minutes, pythondjango

Source: Internet
Author: User

Python programming django implements the same ip address can only be registered once in 10 minutes, pythondjango

Many friends may have this problem. It is said that an IP address can be registered once within 10 minutes to Prevent Users From repeatedly registering the IP address.

Logic:

Take the ip address and check whether the ip address exists in the database. The difference between the current time and the last access time of the ip address is determined. If the ip address is less than 600, it cannot be registered. On the logon interface, it can be registered if the ip address is greater than 600, design a database to store the IP address and access time,

Class Ip (models. model): ip = models. charField (max_length = 20) time = models. dateTimeField () class Meta: verbose_name = u'access time' verbose_name_plural = verbose_name def _ str _ (self): return self. ip

Then go

Python manage. py makemigrations
Python manage. py migrate

In this way, we can update our database and run our project to view the data of our newly registered ip address in the background.

We can design our code according to the previous logic,

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' can only be registered once in 10 minutes '}) ip_c.time = datetime. datetime. now () ip_c.save () return render (request, 'reg.html ') Does T 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' the user name should be 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' indicates whether the mobile phone number format is correct '}) shouji = User. objects. filter (mobile _ exact = shouj) if shouji: return render (request, 'reg.html ', {'msg': U' the mobile phone number already exists '}) youjian = request. POST ['email '] if len (getMailAddFromFile (youjian) <= 0: return render (request, 'reg.html', {'msg ': U' whether the mailbox format is correct '}) use = User. objects. filter (username _ exact = username) if use: return render(request,'reg.html ', {'msg': U' user name already exists '}) 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 upload', {'msg ': u' check whether the password is consistent '}) return render(request,'reg.html ')

In fact, our entire process has been built. After the code is released, some friends will ask how your code is different from what I use,

We are all functional programming. In fact, it is very simple. We can integrate the View class to implement our object-oriented programming. In the url, we only need to write our code like this.

url(r'^reg$', RegView.as_view(),name='reg'),

In this way, we can limit the number of times that the same ip address is registered for a period of time.

Summary

The above is all the content that can only be registered once in 10 minutes for Python programming django to implement the same ip address. I hope it will help you. If you are interested, please refer to this site for a brief introduction to several functions in Python, Python timer instance code, and Python network programming details. If you have any questions, please feel free to leave a message, the editor will reply to you in a timely manner.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.