Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top