使用Django

时间:2018-07-14 16:16:52

标签: django python-3.x django-models django-templates django-views

您好Djangonauts,

我想在“订单历史记录”页面(ListView)中添加一些“订单详细信息”(DetailView),请参见下面的图像示例(我已经在photoshop中制作了图像)。我能够获得灰色部分(要显示的订单列表),但无法获得要在此页面中显示的项目详细信息。如果单击View Order Detail,它将转到详细信息页面,在此可以显示所有这些信息。但我也需要ListPage中的一个小摘要,也请参见下面的示例

enter image description here

下面是我的 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

我什至尝试过 # context['order_items'] = OrderItem.objects.filter(order__id=self.kwargs.get("order.id"))

下面是我的 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.ForeignKey(Product, on_delete=models.CASCADE)
    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

我还在下面附上我的产品型号。以防万一,如果您需要查看

class Product(models.Model):
    name = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    image = models.ImageField(upload_to='product', blank=True)
    stock = models.IntegerField(default=True)

下面是我的模板。此问题不需要它。我相信我在views.py中做错了。但是我以防万一。

    {% 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 %}

任何有关如何进行这项工作的建议

1 个答案:

答案 0 :(得分:1)

在您的模板中,您尝试显示不存在的图像,因为您的产品字段是CharField,因此没有与之关联的图像字段。您的OrderItem模型上的产品是否应该是产品模型的外键?

请注意,您需要在模板中的图像上调用url()方法,即{{product.image.url}}。请参阅FileField上的文档: https://docs.djangoproject.com/en/2.0/ref/models/fields/#filefield

在您的模板中,尝试将item.price或数量显示为文本字段,以证明您的查询集有效。