Контекстные данные не передаются в шаблоны Django
Мое представление корзины. Я передал контекст при возврате, но он не отображается в шаблонах. Если я выведу
def cart(request):
total = 0
quantity = 0
cart_items = None
tax = None
grand_total = None
try:
cart = Cart.objects.get(cart_id=_cart_id(request))
cart_items = CartItem.objects.filter(cart=cart, is_active=True)
for cart_item in cart_items:
total += (cart_item.product.price * cart_item.quality)
quantity = cart_item.quality
tax = (total / 100) * 2
grand_total = total + tax
except:
pass
context = {
'total': total,
'quantity': quantity,
'cart_items': cart_items,
'tax': tax,
'grand_total': grand_total,
}
return render(request, 'c.html', context)
Html Шаблон. Здесь я создал цикл for для получения элементов из массива. Но он не показывает ни одного объекта из массива. Но всегда выводит строку "Item" строку для каждого объекта. {% extends 'base.html' %} {% load static %}
{% block content %}
{% extends 'base.html' %} {% load static %}
{% block content %}
{% for item in cart_items %}
item.product.product_name
{% endfor %}
{% endblock %} {% endblock %}
Ваш код не корректен, я думаю. Попробуйте следующее:
{% extends 'base.html' %}
{% load static %}
{% block content %}
{% for item in cart_items %}
{{item.product.product_name}}
{% endfor %}
{% endblock %}