44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from django.shortcuts import render
|
|
from django.http import HttpResponse, JsonResponse
|
|
from django.contrib.auth import get_user_model
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
# Create your views here.
|
|
|
|
|
|
def index(request):
|
|
username = request.GET.get('username')
|
|
password = request.GET.get('password')
|
|
print(username, password)
|
|
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
|
|
|
|
|
|
def status_500(request):
|
|
return HttpResponse(status=500)
|
|
|
|
|
|
def status_401(request):
|
|
return HttpResponse(status=401)
|
|
|
|
|
|
@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)
|
|
print(u)
|
|
result = dict()
|
|
return JsonResponse(result)
|
|
else:
|
|
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
|
|
else:
|
|
return JsonResponse({})
|