我该如何处理MultiValueDictKeyError?

时间:2013-02-01 07:20:05

标签: django django-forms

我正在处理我的应用程序的结帐表单但是当我提交时,Django会返回MultiValueDictKeyError例外。

Traceback:
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/mats-invasion/projects/f4l/f4l/live/views.py" in show_checkout
  33.           if postdata['submit'] == 'place_order':
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/utils/datastructures.py" in __getitem__
  258.             raise MultiValueDictKeyError("Key %r not found in %r" % (key, self))

Exception Type: MultiValueDictKeyError at /orders/checkout/
Exception Value: "Key 'submit' not found in <QueryDict: {u'delivery_address': [u'klab Telecom House,6th floor'], u'billing_address': [u'klab 6th telecom house'], u'billing_city': [u'kigza'], u'phone': [u'+2500855598432'], u'billing_name': [u'mar'], u'credit_card_type': [u'VISA'], u'credit_card_expire_month': [u'01'], u'credit_card_number': [u'4007000000027'], u'delivery_name': [u'mar'], u'credit_card_cvv': [u'567'], u'credit_card_expire_year': [u'2016'], u'csrfmiddlewaretoken': [u'UGu4G75x2CxPFfnUdGSnVZzueedNxtu6'], u'delivery_city': [u'kigali'], u'email': [u'marxmass@gmail.com']}>"

以下是表单视图:

def show_checkout(request):
    if order.is_empty(request):
        cart_url = urlresolvers.reverse('order_index')
        return HttpResponseRedirect(cart_url)
    if  request.method == 'POST':
        postdata = request.POST.copy()
        form = CheckoutForm(postdata)
        if form.is_valid():
            response = checkout.process(request)
            order_number = response.get('order_number',0)
            error_message = response.get('message','')
            if order_number:            
                request.session['order_number'] = order_number
            if postdata['submit'] == 'place_order':
                reciept_url = urlresolvers.reverse('checkout_reciept')
                return HttpResponseRedirect(reciept_url)
    else:
        form = CheckoutForm()
    context = {
        'form':form,
    }
    return render_to_response('checkout/checkout.html',context,context_instance=RequestContext(request))

以下是我的模板中的内容,

<input type="submit" value="Place order" class="submit" />

感谢任何帮助..

1 个答案:

答案 0 :(得分:4)

更改您的提交输入,使其名称为submit,否则不会进入发布数据。

<input type="submit" value="Place order" class="submit" name="submit" />

此外,值为'Place order',而不是'place_order'