tags:

views:

58

answers:

2

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__

    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

A: 

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.

John Machin
+2  A: 

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.

Amarghosh
@Amarghosh Thank but it still shows same error...
Ansh Jain
did you set `SESSION_SAVE_EVERY_REQUEST` to `True`? Are you sure the user is in fact logged in?
Amarghosh
It works sorry for the last reply actually i have install 2 versions of python and made the changes in wronge one thats why it doesnt work that but now its working.Thanks :)
Ansh Jain