Django:用于渲染的Elif语法

时间:2015-09-18 09:00:14

标签: python django django-views django-class-based-views

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

然而,我似乎无法让他们两个一起工作!

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'"

我哪里出错了?

1 个答案:

答案 0 :(得分:1)

尝试:

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

而不是:

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

两种情况。