标签:too hash hide one except admin none efi conf
rom django.conf.urls import url from django.contrib import admin from app02 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^auth/‘, views.auth.as_view()), ]
from django.shortcuts import render from rest_framework.views import APIView from django.http import JsonResponse from app02.models import * # Create your views here. #---------------------生成随机字符串 def md5(user): import hashlib import time tt=str(time.time()) m=hashlib.md5(bytes(user,encoding=‘utf-8‘)) m.update(bytes(tt,encoding=‘utf-8‘)) return m.hexdigest() class auth(APIView): def post(self,request): ret = {‘code‘: 1000, ‘msg‘: None} try: user=request._request.POST.get(‘username‘) pwd=request._request.POST.get(‘password‘) obj=UserInfo.objects.filter(username=user,passoword=pwd).first() if not obj: ret[‘code‘]=1001 ret[‘msg‘]=‘用户名密码错误‘ token=md5(user) UserToken.objects.update_or_create(user=obj,defaults={‘token‘:token}) ret[‘token‘] = token#给用户返回到postman里 except Exception as e: ret[‘code‘]=1002 ret[‘msg‘]=‘请求异常‘ return JsonResponse(ret)
from django.db import models # Create your models here. class UserInfo(models.Model): user_type_choices=( (1,‘普通用户‘), (2,‘VIP‘), (3,‘SVIP‘), ) user_type=models.IntegerField(choices=user_type_choices) username=models.CharField(max_length=32) passoword=models.CharField(max_length=32) class UserToken(models.Model): user=models.OneToOneField(to=‘UserInfo‘) token=models.CharField(max_length=32)
postman执行:
标签:too hash hide one except admin none efi conf
原文地址:https://www.cnblogs.com/wanghuaqiang/p/9063023.html