我得到的错误是我的View没有返回HttpResponse对象。它没有返回

时间:2016-03-26 13:01:43

标签: python django django-views

def my_view(request, my_id):
    myItem = Item.objects.get(pk=item_id)
    context = {'myitem': my item}
    #This is where I send a POST request from a button named myButton with a value="OK".
    if request.method == "POST":
        if 'OK' in request.POST:
            return render(request, 'url1', context)`enter code here`
    else:
        return render(request, 'url2', context)

以上是我的代码。如果我打印request.POST,它实际上在字典中的值为OK。然而,Python不执行if子句。

request.POST的输出是:

<QueryDict: {u'submit': [u'OK'], u'csrfmiddlewaretoken': [u'#####']}>

1 个答案:

答案 0 :(得分:1)

AFAIK,request.POST有字典结构;它具有关键和价值。假设request.POST具有{'key': 'OK'},则条件语句适用于

if request.POST['key'] == 'OK':

或者这会更好

if request.POST.get('key') == 'OK':

因为如果request.POST.get没有这样的密钥,None会给Crashlytics was applied to a project without an Android plugin. Please make sure the Crashlytics plugin is applied after the appropriate Android plugin for your project.