gpt4 book ai didi

django - 将上下文添加到 django admin changelist_view

转载 作者:行者123 更新时间:2023-12-05 02:45:07 25 4
gpt4 key购买 nike

我正在尝试在显示更改列表时向 Django 管理 View 添加一些上下文。

我有这门课

class LeadStatSummaryAdmin(admin.ModelAdmin):
change_list_template = 'admin/stats/leadstatsummary/change_list.html'

def get_queryset(self, request):
qs = super().get_queryset(request)
query=Q()
if 'from_date' in request.GET:
from_date = datetime.datetime.strptime(request.GET['from_date'], '%d/%m/%Y').strftime('%Y-%m-%d')
to_date = datetime.datetime.strptime(request.GET['to_date'], '%d/%m/%Y').strftime('%Y-%m-%d')
query = Q(date_of_lead__gte=from_date, date_of_lead__lte=to_date)

return qs.filter(query)

def changelist_view(self, request, extra_context=None):
response = super().changelist_view(
request,
extra_context=extra_context,)

qs = self.get_queryset(request)

response.context_data['date_form'] = DateForm(request.GET or None)
response.context_data['data'] = qs. \
values('type_of_lead', 'may_contact_provider', 'type_of_care_care_home', 'type_of_care_home_care',
'type_of_care_live_in_care', 'type_of_care_retirement_village') \
.order_by('type_of_lead', 'type_of_care_care_home', 'type_of_care_home_care',
'type_of_care_live_in_care', 'type_of_care_retirement_village','may_contact_provider') \
.annotate(count=Count('type_of_lead'))

return response

这提供了一个日期表单,我可以使用它来过滤查询集。从菜单调用时运行良好(因此没有日期表单)但是当我输入日期并提交时出现此错误

'HttpResponseRedirect' object has no attribute 'context_data'

指的是这行代码

response.context_data['date_form'] = DateForm(request.GET or None) 

我不明白为什么这会导致错误以及如何解决。你能帮忙吗

最佳答案

对于任何感兴趣的人,我解决了使用模板标签的问题

@register.filter
def get_lead_summary(qs):
return qs.values('type_of_lead', 'may_contact_provider', 'type_of_care_care_home', 'type_of_care_home_care',
'type_of_care_live_in_care', 'type_of_care_retirement_village') \
.order_by('type_of_lead', 'type_of_care_care_home', 'type_of_care_home_care',
'type_of_care_live_in_care', 'type_of_care_retirement_village','may_contact_provider') \
.annotate(count=Count('type_of_lead'))

这被称为,例如,{% for row in cl.result_list|get_lead_summary %}

关于django - 将上下文添加到 django admin changelist_view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66119287/

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