Monday, August 4, 2014

html response before saving object

Hi Guys, 

I've update my site from django 1.3 to 1.5
The site works fine, except for a view, the template renders before the obj.save() in the view:

def cart(request):
    try:
        cart = Cart.objects.prefetch_related('cartitem_set__product__category').get(id=request.session['cart_id'])
    except (KeyError, Cart.DoesNotExist) as e:
        cart = Cart()
        cart.save()
        request.session['cart_id'] = cart.id

    if 'add' in request.GET:
        try:
            product = Product.objects.get(slug=request.GET['add'], active=True)
            cart_item = CartItem.objects.get(product=product, cart=cart)
        except CartItem.DoesNotExist:
            cart_item = CartItem(product=product, cart=cart)
            cart_item.save()
        except Product.DoesNotExist:
            pass

    response_args = {'cart': cart}
    return render_to_response('products/cart.html', response_args, context_instance=RequestContext(request))

I don't know why this happens
any help? something has changed? now the database save is async?

--
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+unsubscribe@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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/867c7f42-fef9-4eba-8acb-e5d552bb7377%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment