0

I have been reading on django session framework and am not really getting it. i am creating an object and am wondering how i can be able to use django sessions in the view. this is how o create an object.

def show_checkout(request):
    if order.is_empty(request):
        cart_url = urlresolvers.reverse('order_index')
        return HttpResponseRedirect(cart_url)
    if request.method == 'POST':
        postdata = request.POST.copy()
        form = forms.CheckoutForm(request.POST,postdata)
        if form.is_valid():
            anon_user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
            obj = form.save(commit=False)
            obj.created_by = anon_user
            obj.modified_by = anon_user
            obj.save()
            if postdata['submit'] == 'place order':
                reciept_url = urlresolvers.reverse('checkout_reciept')
                return HttpResponseRedirect(reciept_url)
    else:
        form = forms.CheckoutForm
    context = {
        'form':form,
    }
    return render_to_response('checkout/checkout.html',context,context_instance=RequestContext(request))

Any help is appreciated. Beginner programmer.

Mats_invasion
  • 117
  • 1
  • 2
  • 12

2 Answers2

2

To hold a value in session, you assign it by key:

request.session[key] = value

To retrieve a value from session you read it by key:

foo = request.session[key]
Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
1

If I've understood correctly what you're asking, you probably just need to do this after obj.save():

request.session['obj_id'] = obj.id

And in the next view you can access that key again to get the object id, then retrieve the object from the db.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Hi Daniel, I tried what you say but it isn't working, when I redirect, in the other view I get an empty session, any idea? – educampver Sep 07 '13 at 22:53