我在网上开店。在购物车(views.show_cart)视图中,当前显示“产品”,“单价”,“数量”,“总价”,然后是允许您更新数量的表单,以及每个的“更新”按钮购物车中的产品。
我希望表单的初始值与购物车中已有的每件商品(产品)的“数量”相匹配。但是,我不能让这个工作。我尝试通过cart_items(在views.show_cart中)循环获取item.quantity,然后将其作为表单中的初始值添加,但每个项目的数量显示最后一项的数量。换句话说,购物车中有2个商品,一个数量为3,另一个数量为7.两个商品形式均显示为7。
#the form
PRODUCT_QUANTITY_CHOICES = [(i, str(i)) for i in range(1, 21)]
class ProductAddToCartForm(forms.Form):
quantity = forms.TypedChoiceField(choices=PRODUCT_QUANTITY_CHOICES, coerce=int)
update = forms.BooleanField(required=False,initial=False,widget=forms.HiddenInput(attrs={'class': 'form-control'}))
#cart view (cart_id.views.show_cart)
def show_cart(request):
cart_id = _cart_id(request)
cart_items = CartItem.objects.filter(cart_id=cart_id)
for item in cart_items:
q = item.quantity
print q #prints to shell w/correct quantities.
add_product_form = ProductAddToCartForm(initial={'quantity': q})
context = {'cart_items': cart_items, 'add_product_form': add_product_form}
这是涉及的另一个视图(cart_id.views.cart_add)
def cart_add(request, product_slug):
cart_id = _cart_id(request)
p = get_object_or_404(Product, slug=product_slug)
cart_products = CartItem.objects.filter(cart_id=cart_id)
product_in_cart = False
if request.method == 'POST':
form = ProductAddToCartForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
quantity = cd['quantity']
update = cd['update']
for cart_item in cart_products:
if cart_item.product.id == p.id:
cart_item.quantity = quantity
cart_item.save()
product_in_cart = True
if not product_in_cart:
ci = CartItem()
ci.product = p
ci.quantity += quantity
ci.cart_id = cart_id
ci.save()
return redirect('cart:show_cart')
模板(cart.html)
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}Shopping Cart{% endblock %}
{% block content %} {% endblock %}
{% block extra_content %}
<div class="container">
<div class="col-md-12">
h2>Cart</h2>
<caption><b>Your Shopping Cart:</b> {{ cart_items_count }} Items In Your Cart.</caption>
<hr>
{% for item in cart_items %}
<table class="table table-striped">
<thead>
<tr>
<th>Product</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="{{ item.product.get_absolute_url }}">{{ item.product }}</a></td>
<td>${{ item.product.price }}</td>
<td>{{ item.quantity }}</td>
<td>{{ item.total }}</td>
<td>
<div class="form-group">
<form action="{% url 'cart:add' item.product.slug %}" method="POST">
{% csrf_token %}
<div class="col-xs-3">
{{ add_product_form.quantity }}
</div>
{{ add_product_form.update }}
<input class='btn btn-success btn-sm' type='submit' value='Update'/>
</form>
</div>
</td>
</tr>
</tbody>
</table>
{% empty %}
<h3>You Have Nothing In Your Cart.</h3>
{% endfor %}
</div>
</div>
{% endblock %}
模特:
class CartItem(models.Model):
cart_id = models.CharField(max_length=50, db_index=True)
date_added = models.DateTimeField(auto_now_add=True)
quantity = models.IntegerField(default=1)
product = models.ForeignKey(Product)
如果需要,这是github上的完整代码:https://github.com/chriskavanagh/ecommerce/tree/master/src。我为这篇文章中的错误间距道歉。