0
votes

I am trying to use session to pass some data from one page to another page. Here is the code i wrote in ajax.py.

def save_cookie(request, query):
    request.session['query'] = query

But when i call this dajaxice function.An error will occurred. As we all know that when we try to use dajaxice in html page, the error msg always is "sth goes wrong". I tried to debug save_cookie, but the mock request object i created has no session attr. However, if i do request.session="blah", it worked. If I directly use save_cookie(request,query). It will pop up the error msg that request object has no attr seesion...

The code is right straight forward. I didn't see any mistake in it. Does anyone know the cause?

2

2 Answers

0
votes

Never used dajaxice / dajax so I can't really help here. Just a few points:

  • did you enable (and properly configue) the session support ? https://docs.djangoproject.com/en/1.3/topics/http/sessions/

  • you can use the logging module (or a plain "print" statement but then you won't have the whole traceback) to trace the exception, ie :

    def save_cookie(request, query): try: request.session['query'] = query except Exception, e: print e raise

The output of the print statement should now appear in the shell you started the dev server from (assuming you're working with the dev server... you ARE workin with the dev server, aren't you ?)

  • still using the dev server, you can use pdb to switch to interactive debugging:

    def save_cookie(request, query): import pdb; pdb.set_trace() request.session['query'] = query

then try to access the url in your browser, switch back to your shell and you're in a pdb session where you can inspect the request and (if there's one) request.session object etc.

NB : don't do this if running behind Apache or any other web server - only with the builtin dev server.

  • "request.session='blah'" will create the "session" attribute on the "request" object if it doesn't exist (and possibly replace the real "session" object if it already existed), so it's neither a valid test nor something sensible to do

My 2 cents...

-1
votes

Disclaimer: I don't know anything about dajaxice.

The following will work on a mock request object:

def save_cookie(request, query):
    if not hasattr(request, 'session'):
        request.session = dict()
    request.session['query'] = query