Sanjay schrieb:
> I have a form with a FileField. After a validation error or exception
> occurs, the field is not preserving the old path name, but is becoming
> blank. Pondering whether it the expected behaviour, or I might be
> missing something...
Yes, this is normal for <input type="file" /> elements. You cannot pre-fill
such a field from you code as a security measure.
As a workaround, if the error in the form was not related to the file upload,
i.e. you received a valid file, you can save it to a temporary space (e.g. the
cherrypy session) and stick it's name into an additional hidden field and
somehow inform the user that the file is already uploaded.
This is a very crude hack I used in one of my projects ('upload' is the name of
the file-input form field, 'attachment' is the name of the hidden form field):
def handle_upload(self, **values):
[... do validation here or with a decorator]
# handle file upload: if there is an upload, store it in the session
upload = values.get('upload')
if getattr(upload, 'filename', None):
filedict = dict(
data = upload.file.read(),
filename = upload.filename,
type = upload.type
)
values['attachment'] = upload.filename
del values['upload']
cherrypy.session['upload'] = filedict
# if there is a filename, look in the session data if we have
# a matching file object stored there and use that
elif values.get('attachment'):
upload = cherrypy.session.get('upload', {})
if upload.get('filename') != values.get('attachment'):
errors['upload'] = _(
u'Specified file does not exist on server. Upload it first.')
del values['attachment']
[...]
Chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---