John Doe
John Doe

Reputation: 145

Django template: submit form after file upload

Hey I have Django model (CharField,CharField,FileField) with fields creating a form like this:

## Form ##

 - To (text input)
 - Comment (text input)
 - Attachment (file input)

[submit button]

Currently the file is being uploaded when I submit the form. How can I make 'upload button' so the files can be uploaded before I submit the form?

Upvotes: 1

Views: 226

Answers (1)

Arpit Singh
Arpit Singh

Reputation: 3467

since your question was a little vague, so is this answer

You will have to upload file over ajax first, then get the pk of that uploaded file in ajax response.. Hit submit. Then again get the instance of that file using its pk.

file_instance = File.objects.get(pk=pk)

And save the

Form(initial={'file': file_instance}, data=request.POST).save()

And you might wanna take upload field out of your form to avoid double submission

Upvotes: 2

Related Questions