My HTML page looks something like below:
The post function used is for upload the file with edited data..!
Im trying to get the option value from HTML page to my Django view.
<div><h3 style="text-align: center">Bulk Upload</h3></div>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="col-md-4">
<select class="form-select form-control" style="padding: 3px; border-radius: 1px;" id="option" name="option">
<option selected value="0"> Please select operation</option>
<option value="1">Insert New Source Client</option>
<option value="2">Update Volcker Attribute</option>
</select>
</div>
<div class="col-md-2">
{% csrf_token %}
<button
id="downloadBtn"
type="submit"
title="Download CSV Template"
onclick="export_template()"
class="btn btn-primary"
style="height: 34px; width: 100px"
>
Download
</button>
</div>
My javascript function is defined as below:
function export_template(){
let option = document.getElementById('option').value;
console.log(option)
if (option!=""){
window.location.href="export_template/option="+option;
}
}
The view defined is as below where im trying to fetch “option” from JS script please advice
def export_template(request, post):
#option = int(request.POST.get("option"))
option = int(request.POST.get("option"))
if option == 2:
file = open('C:/Users/Desktop/New Project (2) (1)/New Project/uploadcsv/media/bulk_upload/Update_Client_YYYYMMDD.csv', 'r')
response = HttpResponse(file, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=Update__YYYYMMDD.csv'
return response
How to pass JS script value as parameter to my Django view..!`