程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django writes a registration login interface and solves the problem of repeated login

編輯:Python
 The first step is in models Create a table structure below :
class NewUser(models.Model):
phone = models.CharField(verbose_name=' cell-phone number ', max_length=11, unique=True)
email = models.EmailField(verbose_name=' mailbox ', max_length=32, unique=True)
nick = models.CharField(verbose_name=" user name ", max_length=50)
password = models.CharField(verbose_name=" password ", max_length=32, null=True, blank=True)
class Meta:
verbose_name=' User table '
verbose_name_plural=verbose_name
db_table='new_user'
def __str__(self):
return self.nick
After the view Write interface in view
def register(request):
email = request.POST.get('email')
nick = request.POST.get('nick')
phone = request.POST.get('phone')
password = request.POST.get('password')
password2 = request.POST.get('password2')
if email and nick and phone and password and password2:
if password != password2:
return JsonResponse({"code": -1, 'mas': " The two passwords are not the same "})
user_email = models.NewUser.objects.filter(email=email)
user_phone = models.NewUser.objects.filter(phone=phone)
if user_email.exists():
return JsonResponse({'code': -1, 'msg': 'email There is '})
if user_phone.exists():
return JsonResponse({'code': -1, 'msg': 'phone There is '})
new_password = md5(password)
uesr_save = models.NewUser(email=email, nick=nick, phone=phone, password=new_password)
uesr_save.save()
return JsonResponse({'code': 0, 'msg': 'success'})
else:
return JsonResponse({'code': -1, "msg": " Can't be empty "})

Last in user Inside to load

The above is the registered interface , Write the login interface below , And generate token, To produce token There is redis inside

First step , To configure redis:

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
'DECODE_RESPONSES':True # This means to return the string by default
}
},
"user": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
}

Put this configuration in settings Next

Then use pip install django_redis Two installation redis modular

stay view When writing interfaces in the view layer . First import   import  django_redis You can use it

The solution to this problem is , towards redis Add one key

def login2(request):
username = request.POST.get('username')
password = request.POST.get('password')
if username and password:
user = models.NewUser.objects.filter(Q(email=username) | Q(phone=username)) # amount to or
if not user.exists():
return JsonResponse({'code': -1, 'msg': ' User name input error '})
if user.first().password != md5(password):
return JsonResponse({'code': -1, 'msg': ' Wrong password '})
user_key = 'seeeion_id:%s ' % user.first().phone
res = django_redis.get_redis_connection()
redis_token = res.get(user_key)
print("redis_token",redis_token)
if redis_token:
return JsonResponse({'code': 0, 'msg': ' It's already logged in , No need to log in repeatedly ', "token": redis_token.decode()})#decode() Is converted to a string
token = md5(str(time.time()) + username)
user_dict = json.dumps(model_to_dict(user.first()))
expire_time = 60 * 60 # session Due time
res.set(user_key, token, expire_time)
res.set(token, user_dict, expire_time)
return JsonResponse({'code': 0, 'msg': ' Login successful ', 'token': token})
else:
return JsonResponse({'code': -1, "msg": "not null"})

After the interface is written , When configuring a route, you can use


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved