Including DetailView context in ListView page in the Order history page using Django

21 views
Skip to first unread message

Samir Tendulkar

unread,
Jul 14, 2018, 12:37:48 PM7/14/18
to Django users



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

enter image description here

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 context

I 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 

Simon McConnell

unread,
Jul 14, 2018, 3:55:10 PM7/14/18
to Django users
item.product.image from your template points to nothing. Product is a CharField so I don't see it having any image attribute.

Try to display some text for item.product or item.price and see how you go.

Samir Tendulkar

unread,
Jul 15, 2018, 11:44:15 AM7/15/18
to django...@googlegroups.com
Thanks I realized I made a mistake there. I changed the models to from
Cjarfield to

product = models.ForeignKey(Product, on_delete=models.CASCADE) Still
stuck on the view
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/092f9c42-3f1f-4cef-a7b4-9c6984013255%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages