I am trying to implement a shopping cart and i have created the following views. The first two, cart_add(request, product_id)
and cart_remove(request, product_id)
add and remove items from a cart stored in a session. Each of them then redirects to a cart_detail(request)
view which returns the current content of the cart to a template.
我正在尝试实现购物车,我创建了以下视图。前两个,cart_add(request,product_id)和cart_remove(request,product_id)从存储在会话中的购物车中添加和删除项目。然后,每个视图都重定向到cart_detail(请求)视图,该视图将购物车的当前内容返回给模板。
@require_POST
def cart_add(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
form = CartAddProductForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product, quantity=cd['quantity'], update_quantity=cd['update'])
return redirect('cart:cart_detail')
def cart_remove(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
cart.remove(product)
return redirect('cart:cart_detail')
def cart_detail(request):
cart = Cart(request)
return render(request, 'cart/detail.html', {'cart': cart})
I have added some products to the cart but the json i get looks like so:
我已经添加了一些产品到购物车,但json我看起来像这样:
{'product': '[{"model": "resource_manager.product", "pk": 6, "fields": {"created": "2016-04-17T12:33:43Z", "updated": "2016-04-18T10:45:17Z", "name": "Workbook", "image": "resource_manager/2016/04/18/20130405_171035.jpg"}}]', 'quantity': 12}
{'product': '[{"model": "resource_manager.product", "pk": 1, "fields": {"created": "2016-04-16T04:13:34Z", "updated": "2016-04-17T12:25:45Z", "name": "Radio", "image": ""}}]', 'quantity': 2}
{'product': '[{"model": "resource_manager.product", "pk": 2, "fields": {"created": "2016-04-16T11:15:57Z", "updated": "2016-04-17T12:25:45Z", "name": "Mat", "image": ""}}]', 'quantity': 9}
{'product': '[{"model": "resource_manager.product", "pk": 4, "fields": {"created": "2016-04-17T07:42:54Z", "updated": "2016-04-17T12:25:45Z", "name": "Tablet", "image": ""}}]', 'quantity': 3}
QUESTION: How do I access the the name, of each product within the template cart/detail.html
问题:如何访问模板cart / detail.html中每个产品的名称
Here is what i tried:
这是我试过的:
{% for item in cart %}
{% with product=item.product %}
<tr>
<td>
<a href="{{ product.get_absolute_url }}">
<img src="{% if product.image %}{{ product.image.url }}{% else %}{% static "resource_manager/img/no_image.png" %}{% endif %}">
</a>
</td>
<td>{{ item.name }}</td>
<td>{{ item.quantity }}</td>
<td><a href="{% url "cart:cart_remove" product.id %}">Remove</a></td>
</tr>
{% endwith %}
{% endfor %}
1 个解决方案
#1
0
You are almost there. You just need to iterate through the values of the key product
.
你快到了。您只需要遍历关键产品的值。
Here is one way of doing this:
这是一种方法:
{% for key, value in cart.items %}
{% if key == 'product' %}
{% for product in value %}
<tr>
<td>
<a href="{{ product.get_absolute_url }}">
<img src="{% if product.image %}{{ product.image.url }}{% else %}{% static "resource_manager/img/no_image.png" %}{% endif %}">
</a>
</td>
<td>{{ item.name }}</td>
<td>{{ item.quantity }}</td>
<td><a href="{% url "cart:cart_remove" product.id %}">Remove</a></td>
</tr>
{% endfor %}
{% else %}
{# do something else.. #}
{% endif %}
{% endfor %}
#1
0
You are almost there. You just need to iterate through the values of the key product
.
你快到了。您只需要遍历关键产品的值。
Here is one way of doing this:
这是一种方法:
{% for key, value in cart.items %}
{% if key == 'product' %}
{% for product in value %}
<tr>
<td>
<a href="{{ product.get_absolute_url }}">
<img src="{% if product.image %}{{ product.image.url }}{% else %}{% static "resource_manager/img/no_image.png" %}{% endif %}">
</a>
</td>
<td>{{ item.name }}</td>
<td>{{ item.quantity }}</td>
<td><a href="{% url "cart:cart_remove" product.id %}">Remove</a></td>
</tr>
{% endfor %}
{% else %}
{# do something else.. #}
{% endif %}
{% endfor %}