gpt4 book ai didi

python - 在 get_queryset 中添加新的上下文变量

转载 作者:行者123 更新时间:2023-12-01 07:09:05 26 4
gpt4 key购买 nike

我有一个页面,显示将根据用户选择的月份显示每月收入。

在 HTML 中,有一个包含所有可能的输入。现在在我的

I'm returning a new variable for use in the template, **not a queryset**. I just want to put `{{ revenue }}` in the template.

我读到我不需要发送 post,只需使用 get 我就可以实现这一点。因此,我设置了此表单来捕获选定的月份,但我得到:

TypeError at /ordenes/ingresos
unhashable type: 'slice'

我应该如何在 get_queryset 方法中设置上下文?

Views.py

class OrdersListView(ListView):

model = Order
template_name = "order/revenue.html"
paginate_by = 10

def get_queryset(self):
filter_month = self.request.GET.get('filtromes', '0')
order = self.request.GET.get('orderby', 'created')
context = {}
if filter_month == "0":
paid_orders = Order.objects.filter(status = 'recibido_pagado')
revenue = 0
for order in paid_orders:
revenue += order.total - order.shipping_cost
context['revenue'] = revenue
return context
else:
paid_orders = Order.objects.filter(created__month=filter_month).filter(status = 'recibido_pagado')
#orders_shipping = Order.objects.filter(created__month=9).filter(status = 'recibido_pagado')
revenue = 0
for order in paid_orders:
revenue += order.total - order.shipping_cost
context['revenue'] = revenue
return context

def get_context_data(self, **kwargs):
context = super(OrdersListView, self).get_context_data(**kwargs)
#filter_month = self.request.GET.get('filtro_mes', '0')
context['filtromes'] = self.request.GET.get('filtromes', '0')
context['orderby'] = self.request.GET.get('orderby', 'created')
context['category'] = "catalogo"
return context

HTML:

<!--### Filter ###-->
<div class="row margin-top5 margin-bottom5 ">
<form method="get" action="{% url 'order:ingresos' %}">
<div class="input-group">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Mes</label>
</div>
<select class="custom-select" searchable="Search here.." value={{filtromes}} name="filtromes">
<option value="0" disabled>Elegir mes</option>
<option value="1">Enero</option>
<option value="2">Febrero</option>
<option value="3">Marzo</option>
<option value="4">Abril</option>
<option value="5">Mayo</option>
<option value="6">Junio</option>
<option value="7">Julio</option>
<option value="8">Agosto</option>
<option value="9">Setiembre</option>
<option value="10">Octubre</option>
<option value="11">Noviembre</option>
<option value="12">Diciembre</option>
</select>
<div class="input-group-append">
<input class="btn btn-outline-secondary" type="submit" name="buscar" value="Buscar" style="margin-bottom: 0px;" />
</div>
</div>
</form>
</div>


<!--### Filter ###-->

<h1> {{ revenue }}</h1>

最佳答案

您可以将 object_list 拉入 get_context_data,而不是尝试将上下文强制放入 get_queryset:

def get_queryset(self):
filter_month = self.request.GET.get('filtromes', '0')
if filter_month == "0":
return Order.objects.filter(status = 'recibido_pagado')
else:
return (Order.objects
.filter(created__month=filter_month, status = 'recibido_pagado'))

def get_context_data(self, **kwargs):
context = super(OrdersListView, self).get_context_data(**kwargs)
qs = kwargs.pop('object_list', self.object_list)
order = self.request.GET.get('orderby', 'created')
context = {}
revenue = 0
for order in qs:
revenue += order.total - order.shipping_cost
context['revenue'] = revenue
context['filtromes'] = self.request.GET.get('filtromes', '0')
context['orderby'] = self.request.GET.get('orderby', 'created')
context['category'] = "catalogo"
return context

还可以考虑使用annotateSum和/或aggregate来计算收入

关于python - 在 get_queryset 中添加新的上下文变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58313901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com