On Thu, May 8, 2008 at 3:51 PM, Beals <[EMAIL PROTECTED]> wrote:

> Correction: request.FILES does exist -- it just wasn't getting printed
> out with "print request".  An explicit "print request.FILES" did the
> trick.
>
> However, my question still stands: is form.save(commit=False) supposed
> to be writing the file out to disk?  Is this the newforms way of
> handling uploads, and therefore is checking for request.FILES and
> writing the file out ourselves passé?
>

Does your model have a FileField?  If so, then yes, I'd expect save of a
ModelForm for that model to save the file to disk.  Haven't checked the doc
to see if it explicitly says that will happen, that is just how I would
expect it to behave.

Not sure if/how this is different from oldforms, since I never used them.
But checking request.FILES etc is not necessarily passé, it just depends on
your model setup.  In my own app, I do upload files, but I don't use
FileField (nor ModelForms), so I have to handle writing the uploaded file to
the appropriate place myself.

Karen




>
> On May 8, 3:43 pm, Beals <[EMAIL PROTECTED]> wrote:
> > A while back, I looked for code snippets for uploading files.  Almost
> > every site I found contained something like the following:
> >
> > if 'file' in request.FILES:
> >     file = request.FILES['file']
> >     filename = file['filename']
> >     fd = open('%s/%s' % (MEDIA_ROOT, filename), 'wb')
> >     fd.write(file['content'])
> >     fd.close
> > else:
> >     print "No file attached."
> >
> > This seemed to work -- files were uploaded and the pointers from the
> > model worked fine, so I never thought to look at the logs... until
> > today, and I noticed that even though the uploads were working
> > properly, the "No file attached" message kept showing up.  Drilling
> > in, I found that FILES wasn't even in the request object!  Yet the
> > file was still written to disk... weird.
> >
> > So I started putting breakpoints in to figure out where in my view the
> > file was getting written out.  The fourth line is the culprit:
> >
> > if request.method == 'POST':
> >     form = FooForm(request.POST, request.FILES)
> >     if form.is_valid():
> >         new_artifact = form.save(commit=False)
> >         *BREAKPOINT*
> >
> > So how/why is form.save(commit=False) writing the file out to disk??
> > Is this expected behavior for ModelForms?  (NOTE: in case it matters,
> > I'm using the built-in Django webserver.)
> >
> > -Aaron
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to