get_userid.py 1.3 KB

1234567891011121314151617181920212223242526272829
  1. from django.http import JsonResponse
  2. from django.contrib.auth import get_user_model
  3. from django.contrib.auth.decorators import login_required
  4. from django.views.decorators.csrf import csrf_exempt
  5. @csrf_exempt
  6. def get_user_by_phone_number(request, phone_number):
  7. if request.method == 'GET':
  8. if phone_number:
  9. User = get_user_model()
  10. try:
  11. users_with_phone = User.objects.filter(phone_number=phone_number)
  12. if users_with_phone.exists():
  13. user = users_with_phone.first()
  14. user_info = {
  15. 'id': user.id,
  16. 'username': user.username,
  17. 'email': user.email,
  18. }
  19. return JsonResponse(user_info)
  20. else:
  21. # Если пользователь не найден, возвращаем ошибку 404
  22. return JsonResponse({'error': 'User not found'}, status=404)
  23. except User.DoesNotExist:
  24. return JsonResponse({'error': 'User not found'}, status=404)
  25. else:
  26. return JsonResponse({'error': 'Phone number is missing'}, status=400)
  27. else:
  28. return JsonResponse({'error': 'Only GET requests are allowed'}, status=405)