Views.py
def cartdata(request):
emailid = request.session['emailid']
dressid = cart_details.objects.values_list('dressid').filter(emailid=emailid)
cartdress = array.array('i',[dress_details.objects.all().filter(dress_id=dressid)])
for i in cartdress:
cartdetails = dress_details.objects.all().filter(dress_id=i)
return render(request, 'project/shop.html', {'cartdetails':cartdetails})
商店.html
<div class="single-cart-item">{% for i in cartdetails %}
<a href="#" class="product-image">
<img src="{{ i.dress_image1 }}" class="cart-thumb" alt="">
<!-- Cart Item Desc -->
<div class="cart-item-desc">
<span class="product-remove"><i class="fa fa-close" aria-hidden="true"></i></span>
<span class="badge">{{ i.dress_name }}</span>
<h6>{{ i.dress_type }}</h6>
<p class="size">Size:{{ i.dress_size }}</p>
<p class="color">Color: {{ i.dress_color }}</p>
<p class="price">RS.{{ i.dress_price }}</p>
</div>
</a>{% endfor %}
</div>
数据库2 表中的数据库在那里。 第一个购物车表,其中 3 列 1(卡特ID(主键( 2(电子邮件(会话变量( 3(着装(从梳妆台上取(
第二张礼服表,提供所有着装数据。
我的主要问题是我无法获取整个数据,如何在 djano 中获取。?
您不必遍历购物车,只需在元组中传递 id 并拥有过滤的对象,如下所示:
def cartdata(request):
emailid = request.session['emailid']
dressid = cart_details.objects.values_list('dressid').filter(emailid=emailid)
cartdress = array.array('i',[dress_details.objects.all().filter(dress_id=dressid)])
cartdress = (1,2,3,4) # this should be the tuple of ids which you want to filter
cartdetails = dress_details.objects.all().filter(dress_id=i)
return render(request, 'project/shop.html', {'cartdetails':cartdetails})
现在,在您的 html 部分中,您可以迭代凭据及其项目,如下所示:
{% for carts in cartdetails %}
<div class="single-cart-item">
{% for i in carts %}
<a href="#" class="product-image">
<img src="{{ i.dress_image1 }}" class="cart-thumb" alt="">
<!-- Cart Item Desc -->
<div class="cart-item-desc">
<span class="product-remove"><i class="fa fa-close" aria-hidden="true"></i></span>
<span class="badge">{{ i.dress_name }}</span>
<h6>{{ i.dress_type }}</h6>
<p class="size">Size:{{ i.dress_size }}</p>
<p class="color">Color: {{ i.dress_color }}</p>
<p class="price">RS.{{ i.dress_price }}</p>
</div>
</a>{% endfor %}
</div>
{%endfor}
我希望这有所帮助。