Hi Djangonauts,
I would like to add some Order details(DetailView) in the Order history page(ListView), See image example below ( I have made the image on photoshop). I was able to get the grey part (order list to show) But I am not able to get the item details to show in this page. If I click View Order Detail it goes to detail page where I can show all this. But I need a small summary in the ListPage too see example below
below are my view.py
class OrderHistory(LoginRequiredMixin, ListView):
model = Order
template_name = 'order/order_list.html'
def get_context_data(self, **kwargs):
context = super(OrderHistory, self).get_context_data()
context['order_details'] = Order.objects.filter(emailAddress=self.request.user.email)
context['order_items'] = OrderItem.objects.filter(order=self.kwargs.get("order"))
return contextI even tried # context['order_items'] = OrderItem.objects.filter(order__id=self.kwargs.get("order.id"))
Below are my models.py
from django.db import models
class Order(models.Model):
token = models.CharField(max_length=250, blank=True)
total = models.DecimalField(max_digits=6, decimal_places=2, verbose_name='USD Order Total')
emailAddress = models.EmailField(max_length=100, blank=True, verbose_name='Email Address')
created = models.DateTimeField(auto_now_add=True)
billingName = models.CharField(max_length=350, blank=True)
billingAddress1 = models.CharField(max_length=350, blank=True)
billingCity = models.CharField(max_length=100, blank=True)
billingZipcode = models.CharField(max_length=10, blank=True)
billingCountry = models.CharField(max_length=50, blank=True)
class OrderItem(models.Model):
product = models.CharField(max_length=250)
quantity = models.IntegerField()
price = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='USD Price')
order = models.ForeignKey(Order, on_delete=models.CASCADE)
def sub_total(self):
return self.quantity * self.price
Below are my templates Its not needed for this question. I believe I am doing something wrong in the views.py . But I have it just in case.
{% extends 'base.html' %}
{% load staticfiles %}
{% block body %}
<div>
<div class="text-center">
<br/>
<h1 class="text-center my_title">Tasting Order Purchase History</h1>
<br/>
{% if order_details %}
{% for order in order_details %}
<div class="row order_detail_div" >
<div class="col-md-2">
<b>Order Number: 156{{ order.id }}</b><br/>
<small><em>Order Date: {{ order.created|date:"M d Y" }}</em></small>
</div>
<div class="col-md-1"></div>
<div class="col-md-3 text-left">
<b>Status: Paid</b><br/>
<b>Total items in Order: ()</b>
</div>
<div class="col-md-1"></div>
<div class="col-md-3 order_total_text">
<b>Order Total: ${{ order.total }}</b>
</div>
<div class="col-md-2 view_order_details">
<b><a href="{% url 'order:order_detail' order.id %}">View Order Details</a></b>
</div>
</div>
<!--------------------------This is the code for OrderItem that's not working------->
{% for item in order_items %}
<div class="row order_item_detail_div">
<div class="col-md-2">
a <!--This a does not show either -->
<img src="{{ item.product.image }}" >
</div>
</div>
{% endfor %}
<!--------------------------Code ends here ----------------------------------->
{% endfor %}
{% else %}
<p>
You do not have any orders yet.<br/><br/>
<a href="{% url 'home' %}" class="btn btn-secondary">Add more recipes</a>
</p>
{% endif %}
</div>
</div>
<br/>
<br/>
{% endblock %}Any advise on how I can make this work
Try to display some text for item.product or item.price and see how you go.