0

我正在用 django 建立一个电子商务网站。

我正在创建处理订单的页面。

我想这样当一个项目“交付”时,该订单将位于列表的底部。

在此处输入图像描述

views.py def 订单(请求,小时):

    #changes status of order from submitted to shipped
    if request.method == 'POST':

        SUBMITTED = 1
        PROCESSED = 2
        SHIPPED = 3
        CANCELLED = 4

        order_id = request.POST['order_id']
        this_order = Order.objects.get(pk=order_id)

        if this_order.status == SUBMITTED or this_order.status == PROCESSED:
            this_order.status = SHIPPED

        elif this_order.status == SHIPPED:
            this_order.status = SUBMITTED

        this_order.save()

        return HttpResponseRedirect('/orders/' + hour)



    #Get all orders from the past 24 hours
    tz=pytz.timezone('America/Los_Angeles')
    now_nonaware = datetime.datetime.now()
    now = timezone.make_aware(now_nonaware,tz)

    orders = Order.objects.filter(date__range=[now - datetime.timedelta(hours=20), now]).filter(time=hour)


    #get all orders from every college drop
    revelle_orders = orders.filter(location = "revelle")
    muir_orders = orders.filter(location = "muir")
    marshall_orders = orders.filter(location = "marshall")
    erc_orders = orders.filter(location = "erc")
    warren_orders = orders.filter(location = "warren")
    sixth_orders = orders.filter(location = "sixth")


    orderlocations = {"revelle": revelle_orders, "muir" : muir_orders, "marshall" : marshall_orders,
                        "erc": erc_orders, "warren": warren_orders, "sixth": sixth_orders}
    orders_dict = {"orderlocations" : orderlocations, "hour": hour}
    return render_to_response('orders.html', orders_dict, context_instance=RequestContext(request))

HTML:

Order page for {{hour}}
</br>
</br>
{% for location, orders in orderlocations.items %}

    {% if orders %}
        {{ location|capfirst }}
        <table>
            <tr>
                <td>#</td>
                <td>Name</td>
                <td>Email</td>
                <td>Order</td>
                <td>Delivered</td>
                <td>Phone</td>


            </tr>

            {% for ord in orders %}
                {% for food in ord.orderitem_set.all %}

                <tr>
                    {% if forloop.counter == 1 %}
                        <td>{{forloop.parentloop.counter}}</td>
                        <td>{{ord.full_name}}</td>
                        <td>{{ord.email}}</td>

                    {% else %}
                        <td colspan="3"></td>
                    {% endif %}

                        <td>{{food.name}} (x{{food.quantity}})</td>

                    {% if forloop.counter == 1 %} 

                    <td>
                            <form action="" method="POST">
                                {% csrf_token %}
                                <input type="hidden" name="order_id" value="{{ ord.pk }}"/>
                                <input type="hidden" name="action=" value="toggledelivery"/>
                                <button type="submit">{% ifnotequal 3 ord.status %} Not {% endifnotequal %}Delivered</button>
                            </form>
                    </td>

                    <td>{{ord.phone}}</td>
                    {% endif %}        

                </tr>
                {% endfor %}
            {% endfor %}

        </table>  
    </br>
    </br>
    {% endif %}

{% endfor %}
4

2 回答 2

1

这听起来像是对类中的元选项进行模型排序的工作。

例如,在您的模型类中添加一个名为 Meta 的子类:

class MyModel(models.Model):
    # model fields here...

    class Meta:
        ordering = ['status']

您可以-在状态前添加一个以降序排序。您还可以将多个字段链接在一起进行排序:

class Meta:
    ordering = ['status', '-order_date']
于 2012-09-06T09:25:25.510 回答
1

嗯,因为有这个部分

 <button type="submit">{% ifnotequal 3 ord.status %} Not {% endifnotequal %}Delivered</button>

在您的订单视图中可能是这样的?

orders = Order.objects.filter(date__range=[now - datetime.timedelta(hours=20), now]).filter(time=hour).extra({"is_completed_order": "status == 3"}).order_by('is_completed_order')

例如计算一个额外的 is_completed_order 布尔字段并对其进行排序

于 2012-09-06T09:38:41.150 回答