I am trying to make an chat application using python and django. I almost complete it and its working fine for 8-10 minutes when two persons are chatting after that certain time it shows an error.
here is the traceback : -
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__
开发者_JAVA百科 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
I assume that the user's session got timed-out and hence the request.session
doesn't have partner
or uid
values in it.
Sessions get timed out based on the (lack of) activity on them. Reading a session is not considered activity for expiration purposes. Session expiration is computed from the last time the session was modified. By default, Django only saves to the session database when the session has been modified -- that is if any of its dictionary values have been assigned or deleted. To change this default behavior, set the SESSION_SAVE_EVERY_REQUEST
setting to True. If SESSION_SAVE_EVERY_REQUEST
is True, Django will save the session to the database on every single request.
Try
print 'request.session contains ', repr(request.session)
in your except
suite. Is the dictionary missing anything other than an item with 'partner'
as key? Is it empty? Whatever, try to work out how/why it became like that.
精彩评论