gpt4 book ai didi

python - Django:用于渲染的 Elif 语法

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:17 26 4
gpt4 key购买 nike

我有一个模板,允许我在客户搜索和事件搜索之间来回切换(结果显示在 map 上)。

但是,我似乎无法让他们一起工作!

class MapView(TemplateView):
template_name = "index.html"

def post(self, request, *args, **kwargs):
if request.POST['customer_search']:
self.object = CustomerAccount.objects.search(request.POST['customer_search'])
return HttpResponse(serialize('geojson',self.object), content_type="application/json")
elif request.POST['event_search']:
self.object = Event.objects.search(request.POST['event_search'])
return HttpResponse(serialize('geojson',self.object), content_type="application/json")
else:
raise SuspiciousOperation("Blank Request Received")

在这种情况下,当我尝试执行“事件搜索”时,浏览器控制台会返回以下错误:

500: MultiValueDictKeyError at /discover/
"'customer_search'"

如果我颠倒这些(将事件放在 if 上,将客户放在 elif 上),那么当我尝试运行“客户搜索”时,我会得到相反的结果:

500: MultiValueDictKeyError at /discover/
"'event_search'"

我哪里出错了?

最佳答案

尝试:

request.POST.get('event_search', False)
request.POST.get('customer_search', False)

而不是:

request.POST['customer_search']
request.POST['event_search']

在这两种情况下。

关于python - Django:用于渲染的 Elif 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32647927/

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