1

这是错误:

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']}>

如您所见,QueryDict 中清楚地显示了键“city_image”,那么为什么我会收到错误消息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: {}>"
4

1 回答 1

9

您没有粘贴回溯,但我猜 KeyError 来自request.FILES,而不是request.POST. 这可能是因为您没有包含enctype="multipart/form-data"在表单元素中。

于 2013-11-09T10:56:38.447 回答