add token

This commit is contained in:
baoliang 2020-08-19 22:35:24 +08:00
parent e1d317a812
commit c297c37b70
1 changed files with 47 additions and 0 deletions

47
polls/views.py Normal file
View File

@ -0,0 +1,47 @@
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth import get_user_model
from django.views.decorators.csrf import csrf_exempt
from django_token.models import Token
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
@csrf_exempt
def polls_login(request):
if request.method == 'POST':
phone = request.POST.get('phone')
password = request.POST.get('password')
UserModel = get_user_model()
user = UserModel.objects.filter(first_name=phone).first()
if not user:
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
u = authenticate(request, username=user.username, password=password)
if u is not None:
login(request, u)
token = Token.objects.create(user=myuser)
result = dict()
result['id'] = user.id
result['username'] = user.username
result['email'] = user.email
result['role'] = ','.join(roles)
if teacher:
school = teacher.school.title
r = get_region(school)
if r:
result['regionId'] = r[0]
result['regionName'] = r[1]
result['unitId'] = r[2]
result['unitName'] = r[3]
result['teacher'] = {'name': teacher.name, 'school': school}
return JsonResponse(result)
else:
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
else:
return JsonResponse({})