Opened 16 years ago

Closed 10 years ago

#7808 closed New feature (fixed)

Form Preview does not work with file uploads

Reported by: ian_brasil Owned by: nobody
Component: contrib.formtools Version: dev
Severity: Normal Keywords: FormPreview
Cc: Evstifeev Roman Triage Stage: Accepted
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Gabriel Hurley)

There seems to be no way to bind a form to a Form Preview (using request.FILES)...
i tried ignoring the specific warning not to override in preview.py and tried to override the preview_post() method but this did not work.

Additionally this is not documented on http://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-preview/

Change History (11)

comment:1 by Eric Holscher, 16 years ago

milestone: 1.0
Triage Stage: UnreviewedDesign decision needed

comment:2 by oyvind, 16 years ago

Resolution: wontfix
Status: newclosed

You can't preview files since you can't pass on filefields as a hidden field or fill in filefields in any browser. Filefields will always have to be filled again.

I would not consider this a bug, just something out of scope for formpreview.

comment:3 by anonymous, 15 years ago

Resolution: wontfix
Status: closedreopened

This should be mentioned in the FormPreview documentation.

comment:4 by Adam Nelson, 15 years ago

Component: UncategorizedDocumentation
milestone: 1.0

Moving out of version milestone:1.0 for triage based on the anonymous comment.

comment:5 by Gabriel Hurley, 14 years ago

Description: modified (diff)
Triage Stage: Design decision neededAccepted

My this is an old ticket. I had to update the link to the docs in the ticket description, it was that old.

Anyhow, It seems like a reasonable admonition to add to the Form Preview docs as suggested by anonymous.

comment:6 by ijstokes, 14 years ago

Component: Documentationdjango.contrib.formtools
Keywords: FormPreview added
Needs documentation: set

This is not an impossible mission, having FormPreview support files. I made some reasonable progress in about an hour, but it would probably be 1-3 days to get something really working. Below are my notes on how this could be done. I'm going to try and move this to django.contrib.formtools, although a Documentation update would be appreciated -- I wasted several hours reading through FormPreview and trying to get it setup before I realized this wasn't supported, and then put in the extra time to see if I could make it work. Just saying somewhere "This currently doesn't work for Forms with FileFields" would be great.

Ian

I've spent about an hour on this and realized that it isn't an easy fix, so I'll have to leave it. FormPreview doesn't support forms with files attached. Search for FormPreview in the django-users list and you'll see more of my comments on this. The couple of things I had to do to get part way to making this work:

  1. Need hashing algorithm to skip FileFields. I did this by adding a class attribute "has_exclude" to the ModelForm and then when looping through the form members I added:

exclude = getattr(form,'hash_exclude', [])
for bp in form:

if bp.name in exclude:

continue

In fact, this should be more sophisticated. It should include the *filename* in the hash, but not the file itself. This way the filename forms part of the hash which can be used to recover the "temporary" uploaded file location from the preview stage, and place it into the correct final location. If it isn't part of the hash then the selected file could be manipulated.

  1. Default FormPreview templates need to include enctype="multipart/form-data" attribute on the <form> element.
  1. Need to test form.is_multipart() to see if form should be constructed by:

f = form(request.POST, auto_id=...)

or

f = form(request.POST, request.FILES, auto_id=...)

  1. And now the tricky bit I didn't get to: how to handle uploaded forms. My suggestion would be to place these in a temporary holding location using a directory taken from the hash. The preview stage would then show the results of any file upload validation (since the files would be submitted with the original submit), and if they are OK, just include the filename. If they are not OK, then the form returns to the original submission page with the errors displayed. If the user confirms everything is alright then the second stage submit sends some hidden field which is associated with the original upload file field and this is used to fetch the originally uploaded file from the hash location and then move it to the final location.

Anyway, that is just the general idea. Maybe there is an easier/better way to do this.

If file uploads aren't OK for FormPreview, then the documentation should reflect this so people don't spend time trying it out only to realize this kind of complicated and fancy handling isn't yet in place.

Ian

comment:7 by ijstokes, 14 years ago

Another quick point on what would need to be done in the event of a "partial" submitted form where the file is uploaded but then the form isn't finalized/saved/second-stage-submitted. It would be necessary to clean up these old files. My best idea of how to do this would be to run some kind of "tmpwatch" on the temporary parent directory with a 1-48 hour time limit. This would build it into the operation of the system. Otherwise the developer would need to actually include a tmpwatch cronjob or similar to clean "abandoned" files and directories.

comment:8 by Luke Plant, 14 years ago

Severity: Normal
Type: New feature

comment:9 by Evstifeev Roman, 13 years ago

Cc: Evstifeev Roman added
Easy pickings: unset
UI/UX: unset

comment:10 by Aymeric Augustin, 12 years ago

Status: reopenednew

comment:11 by Greg Chapple, 10 years ago

Resolution: fixed
Status: newclosed

formtools has been extracted into its own repository (​​​​https://github.com/django/django-formtools/). Because of this, the issue tracking for this package has been moved to GitHub issues. I'm going to close this ticket, but I've created a GitHub issue to replace it where the conversation can continue: ​​​​https://github.com/django/django-formtools/issues/23. Thanks!

Note: See TracTickets for help on using tickets.
Back to Top