2013-11-09 43 views
1

這是錯誤:Django的MultiValueDictKeyError

MultiValueDictKeyError at /add_city/ 
"Key 'city_image' not found in <MultiValueDict: {}>" 

這是POST信息:

Variable     Value 
country     u'Bahrain' 
csrfmiddlewaretoken  u'NyuznsyqteRfmgkUC9W0TpZeuuU99WMZ' 
name      u'vv' 
city_image    u'Tuna_8.jpg' 

這是視圖:

class addCity(View): 
    def get(self, request, *args, **kwargs): 
     countries = Country.objects.all() 
     return render_to_response('addCity.html', {'countries':countries}, RequestContext(request)) 

    def post(self, request, *args, **kwargs): 
     name = request.POST['name'] 
     country = Country.objects.get(name=request.POST['country']) 
     image = request.FILES['city_image'] 
     city = City.objects.create(name=name, country=country, image= image) 
     return HttpResponse("success") 

這是QueryDict當我print request.POST

<QueryDict: {u'country': [u'Bahrain'], u'csrfmiddlewaretoken': [u'NyuznsyqteRfmgkUC9W0TpZeuuU99WMZ'], u'name': [u'bb'], u'city_image': [u'user.png']}> 

正如你所看到的,關鍵'city_image'顯然存在於QueryDict中,那麼爲什麼我得到一個錯誤,說key'city_image'找不到?

這是我的HTML文件。

<form method="POST" enctype="multi-part/formdata">{% csrf_token %} 
    <input type="text" name = "name" class="form-control" placeholder="City name" autofocus> 
    <div class="control-group"> 
     <label for="country" class="control-label" style="margin:15px"></label> 
     <div class="controls"> 
      <select name="country" id="country" class="form-control"> 
       <option value="">Select Country</option> 
       {% for country in countries %} 
        <option value="{{country}}">{{country}}</option> 
       {% endfor %} 
      </select> 
     </div> 
    </div> 
    <input name ="city_image" type="file" style="margin:15px"> 
    <input button class="btn btn-lg btn-primary btn-block" id = "create_city_button" type="submit" value = "Create City"> 
</form> 

這是回溯:

Traceback: 
File "/home/hammad/virt_env/virt1/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    115.       response = callback(request, *callback_args, **callback_kwargs) 
File "/home/hammad/virt_env/virt1/local/lib/python2.7/site-packages/django/views/generic/base.py" in view 
    68.    return self.dispatch(request, *args, **kwargs) 
File "/home/hammad/virt_env/virt1/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch 
    86.   return handler(request, *args, **kwargs) 
File "/home/hammad/virt_env/virt1/gccFishing/Locations/views.py" in post 
    388.  image = request.FILES['city_image'] 
File "/home/hammad/virt_env/virt1/local/lib/python2.7/site-packages/django/utils/datastructures.py" in __getitem__ 
    295.    raise MultiValueDictKeyError("Key %r not found in %r" % (key, self)) 

Exception Type: MultiValueDictKeyError at /add_city/ 
Exception Value: "Key 'city_image' not found in <MultiValueDict: {}>" 

回答

9

您沒貼追溯,但我猜KeyError異常是從request.FILES,不request.POST到來。這可能是因爲你沒有在表單元素中包含enctype="multipart/form-data"

+2

哦,我的!我寫了'multi-part/formdata'而不是'multipart/form-data' –

+0

@DanielRoseman,我是MultiValueDict:{}黑色,即使我添加了multipart/form-data,我的代碼可能出錯,我是做Ajax調用爲
'$阿賈克斯({ \t \t \t類型\t: 'POST', 網址\t: '/ saveVendorInfo /', 數據:$( '#vendorInfoForm')序列化(), 加密類型: 「多部分/格式數據」, 成功:功能(響應){ \t \t如果(response.success == '真'){ \t \t \t警報( '成功'); \t \t \t location.href ='/ vendor-list /'; \t \t} \t \t其他 \t \t \t警報( '無法保存'); },' – MegaBytes

相關問題