lazybird / django-carton

A simple and lightweight application for shopping carts and wish lists.
Other
274 stars 101 forks source link

get_tag tag not displaying in template (Django 1.7) #17

Closed gbozee closed 10 years ago

gbozee commented 10 years ago

I can't get the get_tag tag to display my stored cart items. I use django-debug-toolbar and can actually see that the items are actually saved. Here is my template

{% extends 'base.html' %}
{% load carton_tags %}
{% get_cart as cart %}

{% block main_content %}
    {% for item in cart.items %}
        {{ item.tutor.name }}
        {{ item.quantity }}
        {{ item.subtotal }}
    {% endfor %}   
<ul>

    {% for tutor in tutors %}
        <li><a href="{% url 'add_tutor_to_cart' %}?tutor_id={{ tutor.id }}">{{ tutor.display_name }}/ {{ tutor.price }}</a></li>
   {% endfor %}
</ul>
{% endblock %}

and here is my view function

def add(request):
    cart = Cart(request.session)
    tutor = Tutor.objects.get(id=request.GET.get('tutor_id'))
    cart.add(tutor, price=tutor.price)
    return HttpResponse("Added")

def show(request):
    tutors = Tutor.objects.all()
    return render(request, 'show-cart.html',locals())

django-debug-toolbar shows this after adding items to the cart Variable Value u'CART' {u'1': {u'price': u'400', u'product_pk': 1, u'quantity': 2}, u'5': {u'price': u'800', u'product_pk': 5, u'quantity': 1}}

Any help would be appreciated

lazybird commented 10 years ago

Hi and thanks for your comment.

When you use {% get_cart as cart %} the {{ cart }} variable is only available inside your current template block.

This would not work:

{% extends 'base.html' %}
{% load carton_tags %}
{% get_cart as cart %}

{% block main_content %}
    {{ cart }}
{% endblock %}

This should be fine:

{% extends 'base.html' %}
{% load carton_tags %}

{% block main_content %}
    {% get_cart as cart %}
    {{ cart }}
{% endblock %}
gbozee commented 10 years ago

Thanks for the update. Working now