Displaying an uploaded file in a form

46 views
Skip to first unread message

Shawn H

unread,
Jun 5, 2014, 3:12:03 PM6/5/14
to django...@googlegroups.com
I've been trying to understand how to handle file uploads, and I've got the upload portion down.  I have a FileField in my form, process it in my view, and it's saved in the correct location as specified in MEDIA_ROOT and the upload_to attribute of the Model.  My problem is, when I try to bind the uploaded document in a GET request, nothing is working.  I tried following the documentation in the docs, but when I do that I get a 'FieldFile' does not have the buffer interface error.  How do I bind the file to the form so it's available to the user to download or open, while preserving the ability for the user to replace the uploaded file?  Do I have to extract the file from the model field before binding it to the form?  Code below.  Thanks in advance

    form_data = {
'sub_id':s.id,
...
'sub_verified_by':s.verified_by
}
form_file = {'sub_pdr': SimpleUploadedFile('SubmissionPDR.docx', s.pdr_file)}
form = SubmissionEditForm(form_data, form_file)

Tom Evans

unread,
Jun 5, 2014, 4:41:11 PM6/5/14
to django...@googlegroups.com
I think you are trying to redisplay a form that has already been
submitted, with the file there? This is not going to work, mainly
because a file field in a form never has an initial value, but also
because it is the wrong approach.

To display all the fields apart from the file field, you should be
passing the form data to the form as initial, not as data - eg
SubmissionEditForm(initial=form_data) - so that you do not create a
bound form - a form that has been submitted already.

To make the file available to download, you simply add the url to the
file to your template context - s.pdr_file.url - and make a link to it
- no forms involved. Or just add 's', and access it in the template:

{% if s.pdr_file %}
<a href="{{ s.pdr_file.url }}">Download file</a>
{% endif %}

Cheers

Tom

Shawn H

unread,
Jun 6, 2014, 9:28:03 AM6/6/14
to django...@googlegroups.com, teva...@googlemail.com
Ah, OK. The docs don't make that very clear, especially since the normal pattern is that form fields function as display and edit (i.e., you can send initial data to the form).  I guess I expected the widget to handle both the upload function and display the link without additional configuration on my part. Django has spoiled me I guess :-).  Thanks for the explanation

Shawn

Shawn H

unread,
Jun 6, 2014, 10:02:35 AM6/6/14
to django...@googlegroups.com, teva...@googlemail.com
Interestingly, it seems that (at 1.7b4, at least) the widget DOES display the document link if the file is added to the initial data for the form.  I missed deleting that portion of the code from last night, and running it this morning django rendered the widget as shown in the image below. In other words, it behaves like any other form field.



On Thursday, June 5, 2014 3:41:11 PM UTC-5, Tom Evans wrote:
Reply all
Reply to author
Forward
0 new messages