Django的FormView中的文件处理 - request.FILES为空

时间:2014-03-14 10:39:31

标签: django forms file-upload formview

我正在尝试在Django中实现FormView,因为它似乎减少了一些样板代码。其中一个字段是文件上载字段。

class LibraryUploadLastStageForm(forms.Form):

    technician = forms.ModelChoiceField(label="prepeared by" , queryset=Technician.objects.exclude(has_left=True).order_by('name') , required=True)
    subprojects = forms.ModelChoiceField(label="Choose a subproject" , queryset=Subproject.objects.all().order_by('subproject_name') , required=True)
    lab_tracking_excel = forms.FileField(label="Choose the Lab Tracking Excel - protocol details associated with these libraries", required=False)
在views.py中

class LibraryUploadFinalView(FormView):

    template_name = 'sequencing/ChooseSomething.html' 
    form_class = LibraryUploadLastStageForm


    def form_valid(self, form):
        """
        Update the library technician, and create the librarysubproject links
        """

        id_list_str = self.request.GET.get('ids')
        ids = map(int , id_list_str.split(','))

        libraries = Library.objects.filter(id__in=ids)
        print libraries.count()

        cd = form.cleaned_data
        lib_kwargs = {}
        for library in libraries : 
            library.technician = cd['technician']
            if cd['lab_tracking_excel']:
                print cd['lab_tracking_excel']
            library.lab_tracking_excel = self.request.FILES
            library.save()    
            LibrarySubprojectStats(subproject=cd['subprojects'],
                               library=library
                               ).save()


        print "updated %s" % library.name

        return super(LibraryUploadFinalView, self).form_valid(form)

问题是当我在FromView form_valid()方法(我保存详细信息)时,当我在调试器中检查它时,seld.request.FILES为空。

更新:我可以看到该文件存储在form.data下,但我如何从那里获取form.cleaned_data - 哪个是空的?

0 个答案:

没有答案