python - KeyError: Django Losing Session Data Between CBV Redirects -
when redirect view after adding data, stripe customer data, dict added session, lose all of information in session @ redirected view. thus, encounter keyerror when try pop these items.
interestingly, not happen when put other types of information in payment_data dict, list instead of customer object.
i'm not sure what's best way fix problem, given have designed, it's important me customer information confirm view can
- list item
- display customer information user confirmation (censoring sensitive information
- charge card
this code:
class paymentscreateview(formview): def form_valid(self, form): customer = stripe.customer.create(description=""" non-registered user applying features""") customer.save() payment_data = { 'customer': customer } self.request.session['payment_data'] = payment_data self.request.session.modified = true import ipdb;ipdb.set_trace(); return httpresponseredirect(reverse('payments_confirm')) class paymentsconfirmview(templateview): template_name = 'payments/confirm.html' def get_context_data(self, **kwargs): context = super(paymentsconfirmview, self).get_context_data(**kwargs) context['payment_data'] = self.request.session.pop('payment_data') context['feature_data'] = self.request.session.pop('feature_data') return context
i'm still debugging , next step confirm whether issue trying store customer object rather dictionary or list object maybe on can confirm or supply right answer.
from python docs:
list.pop([i])
remove item @ given position in list, , return it. if no index specified, a.pop() removes , returns last item in list.
like rohan says, use get()
:
context['payment_data'] = self.request.session.get('payment_data', false) context['feature_data'] = self.request.session.get('feature_data', false)
Comments
Post a Comment