Проблема отображения выбранного размера из одного шаблона в другой
У меня есть два представления, магазин и корзина, также есть шаблоны для обоих. Я определил 3 варианта размеров в модели продукта и показываю их в форме в магазине. У меня возникают трудности с отображением выбранного размера в cart.html
Вот мнения, которые я имею для них:
def store(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
if request.method == 'POST':
size = request.POST.get('size')
return redirect('cart', size=size)
else:
items = []
order = {'get_cart_total':0, 'get_cart_items':0}
cartItems = order['get_cart_items']
c1 = ProductCollection.objects.get(name='c1')
products = c1.products.all()
context = {
'c1': c1,
'products': products,
'cartItems': cartItems,
}
return render(request, 'store.html', context)
def cart(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
size = request.POST.get('size')
else:
items = []
order = {'get_cart_total':0, 'get_cart_items':0}
cartItems = order['get_cart_items']
context = {
'items': items,
'order': order,
'cartItems': cartItems ,
'size': size
}
return render(request, 'cart.html', context)
Это шаблон store.html:
<div class="row" >
{% for product in products %}
<div class="col-lg-4">
<img class="thumbnail" src="{{product.imageURL}}">
<div class="box-element product">
<h6><strong>{{product.name}}</strong></h6>
<hr>
<form method="post" >
{% csrf_token %}
<label for="size">Select size:</label>
<select id="size" name="size">
<option value="{{ product.size_option_1 }}">{{ product.size_option_1 }}</option>
<option value="{{ product.size_option_2 }}">{{ product.size_option_2 }}</option>
<option value="{{ product.size_option_3 }}">{{ product.size_option_3 }}</option>
</select>
<input type="button" value="Add to Cart" data-product="{{product.id}}" data-action="add" data-size="{{product.size}}" class="btn btn-outline-secondary add-btn update-cart">
</form>
<a class="btn btn-outline-success" href="#">View</a>
<h4 style="display: inline-block; float: right"><strong>${{product.price}}</strong></h4>
</div>
</div>
{% endfor %}
</div>
Это cart.html
<div class="box-element">
<div class="cart-row">
<div style="flex:2"></div>
<div style="flex:2"><strong>Item</strong></div>
<div style="flex:1"><strong>Size</strong></div>
<div style="flex:1"><strong>Price</strong></div>
<div style="flex:1"><strong>Quantity</strong></div>
<div style="flex:1"><strong>Total</strong></div>
</div>
{% for item in items %}
<div class="cart-row">
<div style="flex:2"><img class="row-image" src="{{item.product.imageURL}}"></div>
<div style="flex:2"><p>{{item.product.name}}</p></div>
<div style="flex:1"><p>{{size}}</p></div>
<div style="flex:1"><p>${{item.product.price|floatformat:2}}</p></div>
<div style="flex:1">
<p class="quantity">{{item.quantity}}</p>
<div class="quantity">
<img data-product="{{item.product.id}}" data-action="add" class="chg-quantity update-cart" src="{% static 'images/arrow-up.png' %}">
<img data-product="{{item.product.id}}" data-action="remove" class="chg-quantity update-cart" src="{% static 'images/arrow-down.png' %}">
</div>
</div>
<div style="flex:1"><p>${{item.get_total|floatformat:2}}</p></div>
</div>
{% endfor %}
</div>
</div>
</div>