I am using Django Framework I want to check if the mobile-no is in the database but I have error when I run the code it gives me only False even when the number is exist in database it gives me False can someone help me this is my code
views.py
@csrf_exempt
def forget_password(request):
mobile_no = request.POST.get('mobile_no')
# verify = models.User.objects.all()
# verify = models.User.objects.filter(mobile_no=mobile_no).first()
verify = models.User.objects.filter(mobile_no=mobile_no).exists()
if verify:
return JsonResponse({'bool':True})
else:
return JsonResponse({'bool':False,'msg' : 'mobile_no does not exist!!'})
CodePudding user response:
try This code I think you do a mistake in importing User model
from django.contrib.auth.models import User
@csrf_exempt
def forget_password(request):
mobile_no = request.POST.get('mobile_no')
verify = User.objects.filter(mobile_no=mobile_no)
if verify.exists():
return JsonResponse({'bool':True})
else:
return JsonResponse({'bool':False,'msg' : 'mobile_no does not exist!!'})
CodePudding user response:
As mobile no. for each user is unique, use following view:
from django.views.decorators.http import require_POST
@require_POST
@csrf_exempt
def forget_password(request):
mobile_no = request.POST.get('mobile_no')
try:
models.User.objects.get(mobile_no=mobile_no)
return JsonResponse({'bool':True})
except models.User.DoesNotExist:
return JsonResponse({'bool':False,'msg' : 'mobile_no does not exist!!'})