0

I am having problems with session object in Web2py. On localhost the code below works perfectly fine. Session object stores what I need. However, once I run it like so:

$python web2py.py -i <myhomeip>

The session object is always None. I have on session.clear(), but only if email is sent, and code fails way before that. How can this be that the session is not created/exist?

Ajax calls this function:

def generateCart():
    session.orderdetail = request.vars;
    print "in generateCart() session is", session.orderdetail;
    return None;

Then function cart() accesses session:

print "in cart() session.orderdetail", session.orderdetail;
if (len(session.orderdetail.items()) == 0):
    print "in cart() order was empty"
    redirect(URL('otherfn'));
pass
lOrderDetail = session.orderdetail;

This exact code works when I run as localhost. When I run with -i the print statement above returns None. If I do

print session

I get None.

I don't have session.secure() and don't rely on https for now. I would really appreciate some help on this.

Serge Poele
  • 117
  • 11
  • I just realized something: the object I am storing is JSONified html5 sessionStorage, so it is not "python" object. In the book it says not to store objects in session that are not "python" object. However, would this explain the fact that session object does not exist at all? – Serge Poele Dec 22 '14 at 04:02
  • Kind of talking to myself :). I tried storing session.item = 'hello' and it is also not available in the cart() function. So something else is a problem. – Serge Poele Dec 22 '14 at 04:17
  • I would check what the ajax is sending. – Burhan Khalid Dec 22 '14 at 05:07
  • That's the thing: the controller that ajax calls prints out the information correctly and stores it into the session like in the code above. But session is not accessible globally. I thought session is supposed to exists until the client terminates it (and that's typically the case), but something is not working. I haven't modified any of web2py's code, so I don't really know what else to provide here. – Serge Poele Dec 22 '14 at 14:31
  • Please!!! Does anyone have any thoughts? What changes when I run $python web2py -i ? I mean except admin settings? The same code runs fine on localhost, but with -i session is not created. What changes? – Serge Poele Dec 22 '14 at 15:37

1 Answers1

1

I want to quickly answer this very noob question in case anyone runs into this problem. If moderators think this is not constructive: I understand.

There are several SO posts that helped me. The main one is this: How do I return the response from an asynchronous call?. And the link in the accepted answer in this link.

Basically, the problem was not in what ajax was sending, but when (very noob question). The data was arriving to the server, but not at the right time. By putting in many 'console.log()' and print statements I was able to find that ajax was not arriving. The session was created, but after ajax arrived.

The question remains why on localhost it would work 100% of time, but not on public host. I guess it has something to do with how loopback works on localhost, but I cannot answer that part - I don't know.

Lesson from this: get your basics right (i.e. ajax).

Community
  • 1
  • 1
Serge Poele
  • 117
  • 11