Empty Request.FILES with Django Upload forms

后端 未结 5 687
滥情空心
滥情空心 2020-12-08 02:03

Trying to use a very simple form to upload a file into a new class instance. I am expecting to have both files in request.FILES but it\'s empty. I am on the bun

5条回答
  •  攒了一身酷
    2020-12-08 02:47

    I think your troubles may lie in assigning data to a form without first verifying the the request is POST

    @login_required
    def create_map(request, form_class=WayfinderMapForm, template_name="wayfinder/map create.html"):
      if request.method=='POST':
        wayfinder_map_form = form_class(request.user, data=request.POST, files=request.FILES)
    
        if wayfinder_map_form.is_valid():
          #save your data
          return HttpResponseRedirect(wayfinder_map.get_absolute_url())
    
      else:
        wayfinder_map_form=form_class(request.user)
    
     return render_to_response(template_name, {"wayfinder_map_form": wayfinder_map_form,}, context_instance=RequestContext(request))
    

提交回复
热议问题