How to extract File Object from Django Form FileField

Jubin Thomas picture Jubin Thomas · Aug 28, 2012 · Viewed 7.5k times · Source

I have created a ModelForm with fields, title, file and content. Here file is a FileField(). But I can't call the save() method of this form due to some reasons. So I have to maually created one Model Object and assign cleaned values to that object. Everything worked excpt that FileField. The file is not saving. How can I fix this? Is it the correct method to extract FileField?

Form

class TestForm(forms.ModelForm):
    class Meta:
        model = Test
        fields = ('title','file', 'content',)

Views.py

 form = TestForm(request.POST,request.FILES)
 if form.is_valid():
     content = form.cleaned_data['content']
     file = form.cleaned_data['file']
     title = form.cleaned_data['title']
     fax = Fax()
     fax.title = title
     fax.file = file
     fax.content = content
     fax.save()

Here the file is not saving. How can I fix this? Any help will be appreciated!

Answer

Ani Varghese picture Ani Varghese · Sep 3, 2012

Have u used enctype="multipart/form-data" in your form Seems like the code is fine.