我有一个 django,我正在尝试将一个对象存储在 django session 变量中,并尝试在重定向视图中访问它,但它显示keyerror
如下
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))
结果
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'
因此,我从包含交易详细信息的支付网关取回了一个响应对象,并且我试图将其保存在上述session framework variable
调用response
中。
我正在尝试访问response
在重定向视图PaymentSuccess
中调用的变量request.session['response']
,并得到上述错误。
那么我们如何sessions
在 django 中发送/保存对象?
在上面的响应对象将具有以下形式
{'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,
}}