Does a KeyError say the key (partner) is not in the dict?

I am trying to make a chat application using python and django. I almost complete it and work for 8-10 minutes when two people talk after that specific time when he shows an error.

here is the trace: -

Traceback (most recent call last): File "\Django_chat\django_chat\chat\views.py", line 55, in receive message = chatSession.getMessage(request.session['partner'],request.session['uid'],afterTime) File "C:\Python26\lib\site-packages\django\contrib\sessions\backends\base.py", line 47, in __getitem__ return self._session[key] KeyError: 'partner' 

here is the receive module: -

 def receive(request): # message received by this user chatSession = chat() data = request.POST afterTime = data['lastMsgTime'] try: message = chatSession.getMessage( request.session['partner'], request.session['uid'], afterTime) except: #partnerId = virtual_users.objects.get(id=request.session['uid']).partner print('there is an error in receive request') traceback.print_exc(file=open("/myapp.log","a")) msg = serializers.serialize("json", message) return HttpResponse(msg) 

Please help me :( thanks Ansh J

+4
source share
2 answers

I assume that the user session received a timeout, and therefore request.session has no partner or uid values ​​in it.

Sessions end on the basis of (absence) activity on them. Reading a session is not considered valid for expiration purposes. Session termination is calculated from the last session change. By default, Django saves the session database only if the session has been modified, that is, if any values ​​of its dictionary have been assigned or deleted. To change this default behavior, set SESSION_SAVE_EVERY_REQUEST to True. If SESSION_SAVE_EVERY_REQUEST is True, Django will save the session to the database with every single request.

+3
source

Try

print 'request.session contains ', repr(request.session)

in your except package. Is the dictionary the absence of anything other than an element with 'partner' as the key? Is he empty? Be that as it may, try to figure out how and why it became so.

0
source

Source: https://habr.com/ru/post/1310832/


All Articles