File upload using Django framework

I need insights on how to upload a file on click of a save button.I have to upload a file and also capture user selections and save them(user selections) in a file when I click on "save". And then once the file uploaded successfully run button should get enabled and when I click on run button , uploaded file should get processed as per the user inputs. I have created a simple form and a view to upload a file on click of a submit button,but I dont know how to have a save button before submit.

My View:

def projects_upload(request):
    print(settings.MEDIA_ROOT)
    if request.method=='POST':
        upload_request=UploadFile()
        upload_request.file=request.FILES['file_upload']
        upload_request.save()

Form:

<form action="uploadProject" method="post" enctype="multipart/form-data">
    {% csrf_token%}
    <input type="file" name="file_upload" id="choose_upload_file" value="" accept=".zip,.rar,.7z,.gz,"></br>            
    <input type="submit" class="btn btn-secondary btn-block" value="upload" id="file_upload1">
</form> 

I think you want to add required so:

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" required name="file_upload" id="choose_upload_file" value="" accept=".zip,.rar,.7z,.gz,"></br>            
    <input type="submit" class="btn btn-secondary btn-block" value="upload" id="file_upload1">
</form>

Additionally, I'd recommend you to remove action attribute in the form as Django by default takes current page route, or if you want to redirect to some another page so you should provide path name of view in url tag.

Back to Top