試圖用一個非常簡單的形式將文件上傳到一個新的類實例。我期待在request.FILES
中有兩個文件,但它是空的。我在捆綁的開發服務器上。Django的上傳空Request.FILES形成
被困在這裏,並通過所有相關的問題去了。
wayfinder_map.media_file = request.FILES['media_file']
生成
MultiValueDictKeyError: "Key 'media_file' not found in MultiValueDict: {}>"
模型
class WayfinderMap(models.Model):
""" Way-finding Map Config"""
media_file = models.FileField(upload_to="maps", null=True, blank=True)
wall_file = models.FileField(upload_to="maps_data", null=True, blank=True)
視圖
@login_required
def create_map(request, form_class=WayfinderMapForm, template_name="wayfinder/map/create.html"):
wayfinder_map_form = form_class(request.user, request.POST or None, request.FILES)
if wayfinder_map_form.is_valid():
wayfinder_map = wayfinder_map_form.save(commit=False)
wayfinder_map.media_file = request.FILES['media_file']
wayfinder_map.data_file = request.FILES['data_file']
wayfinder_map.creator = request.user
wayfinder_map.save()
return HttpResponseRedirect(wayfinder_map.get_absolute_url())
return render_to_response(template_name, {
"wayfinder_map_form": wayfinder_map_form,
}, context_instance=RequestContext(request))
模板
<form enctype="multipart/form-data" class="uniForm" id="wayfinder_map_form" method="POST" action="">
<fieldset class="inlineLabels">
{{ wayfinder_map_form|as_uni_form }}
<div class="form_block">
<input type="hidden" name="action" value="create" />
<input type="submit" value="{% trans 'create' %}"/>
</div>
</fieldset>
</form>
我認爲回答這個類似的問題會幫助你:http://stackoverflow.com/q/5895588/1037459 – 2011-11-09 11:05:00