How to print data in Django views.py not in template?
Question:
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.
Answers:
If you are asking how to print data to console then you can just use print(data)
and this will print the data to the console. If you want to print context
then:
order = Order.objects.get(user=request.user, ordered=False)
context['object']=order
print(context)
return render(request,"Orderview.html",context)
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.
The best way is, to put these lines before rendering the template.
import pdb
pdb.set_trace()
return context
Your code should look like this
order = Order.objects.get(user=request.user, ordered=False)
context['object']=order
import pdb
pdb.set_trace()
return context
return render(request,"Orderview.html",context)
When you refresh the page, it will hold/hang and you can run print(context)
, print(order)
or print(context['object'])
statements in the console.
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.
If you are asking how to print data to console then you can just use print(data)
and this will print the data to the console. If you want to print context
then:
order = Order.objects.get(user=request.user, ordered=False)
context['object']=order
print(context)
return render(request,"Orderview.html",context)
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.
The best way is, to put these lines before rendering the template.
import pdb
pdb.set_trace()
return context
Your code should look like this
order = Order.objects.get(user=request.user, ordered=False)
context['object']=order
import pdb
pdb.set_trace()
return context
return render(request,"Orderview.html",context)
When you refresh the page, it will hold/hang and you can run print(context)
, print(order)
or print(context['object'])
statements in the console.