Issue
I am new to Django and I know how to get the data from the database and send to the template. But i am in a situation where I need to print the data from the database in views.py
file
order = Order.objects.get(user=request.user, ordered=False)
context['object']=order
return render(request,"Orderview.html",context)
this order variable contains title,quantity,price of the product
{% for order_item in object.items.all %}
<tr>
<td>1</td>
<td><strong>{{ order_item.item.title }}<</strong><br>{{order_item.item.description }}</td>
<td class="text-center">{{ order_item.quantity }}</td>
<td class="text-right" id="price" >{{ order_item.item.price }}</td>
<td class="text-right" id="discount ">{{ order_item.item.discount_price }}</td>
<td class="text-right" id="subtotal_price">{{ order_item.item.price|subtract:order_item.item.discount_price }}</td>
</tr>
{% endfor %}
This is how I'm able to send all the details to template. But I want to print all this details to the views.py
console.
Solution
in views.py
print order
queryset
order = Order.objects.get(user=request.user, ordered=False)
print(order.quantity) #this will print quantity in the terminal
but if you had a queryset like
order = Order.objects.filter(user=request.user, ordered=False)
for order in orders:
print(order.quantity)
in this case you would just loop through the queryset to print items in the terminal.
Answered By - Mugoya Dihfahsih
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.