History: Error on user = User.objects.get(pk=u.pk) , - DoesNotExist . In general, I can not understand why. Handled this line with an exception, now it does not go into try at all. Where did I go wrong and why does this error go out?

PS The database is fine. In another code, everything works. I also checked the data. The essence of this algorithm is that it searches for applications in two cities, i.e. proud of sending and the city of arrival. After that, he searches in the application! all signed people to it. Then I add an object to the userList ["user"]. Then I do a check: If the number of seats (indicated in Ad) is greater than the users who subscribe to this application, then this application has free places and I add this application to the adList ["name"] list. And return the user. Everything.

 def searchAd(request, ad): j = json.loads(ad) a = Ad(**j) adList = {"name": []} userList = {"user":[]} if not Ad.objects.filter(cityStart=a.cityStart, cityEnd=a.cityEnd): return HttpResponse("null") for ad in Ad.objects.filter(cityStart=a.cityStart, cityEnd=a.cityEnd): for u in User.adPk.through.objects.filter(ad_id=ad.pk): try: user = User.objects.get(pk=u.pk) userList["user"].append(json.dumps({'name': user.name, 'serName': user.serName, 'email': user.email, 'password': user.password,'pk': user.pk}, ensure_ascii=False)) except Exception: pass if ad.countSits >= userList["user"].__len__(): adList["name"].append(json.dumps({'name': ad.name, 'timeStart': ad.timeStart, 'timeEnd': ad.timeEnd, 'cityStart': ad.cityStart,'cityEnd': ad.cityEnd, 'about': ad.about, 'driver': ad.driver, 'price': ad.price,'countSits': ad.countSits, 'pk': ad.pk}, ensure_ascii=False)) return HttpResponse(json.dumps(adList, ensure_ascii=False)) 
  • Well, if there is an error in this line, then look at what is in the u.pk and check for the presence of such a user in the database - BOPOH
  • The request was made in two cities for which there is an application with subscribers. I checked everything carefully. - Dima_Brijatov
  • 2
    If it should work, but it does not work, then in the overwhelming majority of cases, they did not check it carefully enough. Do not look at what cities you are looking for, see what is now in u.pk. Then check for an entry with such a pk in the User table. If something is wrong in pk, then we are looking for a problem elsewhere. But now we look where there is an error, but not above. You may have subscribers, but they are crooked and there are no such users - BOPOH
  • 2
    You know, debug really showed me that I’m not exactly taking those values ​​from u.pk. I will explain for a long time why in u.pk lies not the value that I need, but thank you for being there in these difficult moments of my life. - Dima_Brijatov
  • one
    Use User.DoesNotExist instead of Exception . - MichaelPak

0