I have an django and i am trying to store an object in django session 
varaible, and trying to access that in the redirected view, but its showing 
`keyerror`  as below

    def payment(request):
        if request.method == 'POST':
            form = CardForm(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                response = response_from_payment_gateway(data)
                request.session['response'] = response
                return 
HttpResponseRedirect(reverse('paygate:payment_success'))
        else:
            form = CardForm(initial={'number':'4242424242424242'})
        return render_to_response('payment_form.html',{'form': form})


    def PaymentSuccess(request):
        print request.session['response'],"=================>"
        response = None
        return render_to_response("payment_success.html", 
{'response':response}, context_instance=RequestContext(request))


**Result**

    Internal Server Error: /payment/success/
    Traceback (most recent call last):
      File 
"/home/Envs/app/local/lib/python2.7/site-packages/django/core/handlers/base.py",
 
line 115, in get_response
        response = callback(request, *callback_args, **callback_kwargs)
      File "/home/user/virtualenvironment/apps/app/payment/views.py", line 
120, in PaymentSuccess
        print request.session['response'],"=================>"
      File 
"/home/Envs/app/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py",
 
line 46, in __getitem__
        return self._session[key]
    KeyError: 'response'


So i am getting back a response object from the payment gateway that 
contains the transaction details, and i am trying to save that in `session 
framework variable` called `response` as above.

And i am trying to access the variable called `response` in the redirected 
view `PaymentSuccess` as `request.session['response']`, and getting the 
above mentioned error.

so how can we send/save the objects in the `sessions` in django ?

In above the response object will be of the following form 

    {'status': 'SUCCESS', 'response': <Charge charge id=ch_2OXdxxxxNVw at 
0xb508e76cL> JSON: {
      "amount": 100, 
      "amount_refunded": 0, 
      "balance_transaction": "txxxn_xxxxxxxxO", 
      "captured": true, 
     "currency": "usd", 
      "customer": null, 
      "description": null, 
    }}

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to