How to get selected value in dropdown list, then pass it to views.py
Question:
I’m working on an upload file feature with dropdown and dropzone. But, whenever I submit the uploaded file with selected option, it always says that the selected option is None. I found it None after I printed the nama_bimbingan in views.py. Here it is my code.
url.py
....
url(r'bimbingan/add/upload', mengelola_bimbingan.upload_data_bimbingan, name='add-bimbingan-excel'),
url(r'bimbingan/download-template/', mengelola_bimbingan.bimbingan_template, name='bimbingan-template'),
....
forms.py
class UploadBimbinganForm(forms.Form):
...
...
dropdown_choices = tuple(zip(all_pembimbing, all_pembimbing))
nama_pembimbing = forms.ChoiceField(choices = dropdown_choices)
upload_bimbingan.html
<form method="POST" action="{% url 'app:add-bimbingan-excel' %}" enctype="multipart/form-data">
{% csrf_token %}
<div class="py-3">
<label for="id_nama_pembimbing"> Nama Pembimbing Akademik: </label>
<select class="form-control mb-2" id="id_nama_pembimbing" name="nama_pembimbing" required>
<option value = "" selected="selected">---------</option>
<option value = {{form.nama_pembimbing}}></option>
</select>
</div>
<div id="myDropzone" class="dropzone" drop-zone>
<h6 class="dz-message"> Drop file here or click to upload</h6>
</div>
<div class="py-3">
<a href="{% url 'app:bimbingan-template' %}">Download Template</a><br>
<div class="row justify-content-between py-3">
<div class="col-md-5 mb-1">
<a href="{% url 'app:read-all-bimbingan' %}" class="btn btn-blue-outlined">Batal</a
</div>
<div class="col-md-5">
<input type="submit" value="Simpan" class="btn btn-block btn-blue">
</div>
</div>
</div>
</form>
views.py
@login_required(redirect_field_name='index')
@user_passes_test(only_admin_access)
def upload_data_bimbingan(request):
form = UploadBimbinganForm(request.POST or None)
if request.method == "POST" and 'file' in request.FILES:
nama_pembimbing = request.POST.get('nama_pembimbing')
excel_file = request.FILES["file"]
data = get_data(excel_file, column_limit=1)
bimbingans = data["Bimbingan"]
...
...
if(len(duplicate) == 0):
space_parsed_query = nama_pembimbing.replace(' ', '%20')
cleaned_query = space_parsed_query.replace(',', '%2C')
nip_pembimbing = int(get_data_dosen_by_nama(cleaned_query)[0]["nomor"])
for bimbingan in bimbingans:
if(bimbingan[0] != "NPM"):
new_bimbingan = Bimbingan(nip_pembimbing=nip_pembimbing, npm_mahasiswa=bimbingan[0])
new_bimbingan.save()
return redirect('/app/bimbingan')
else:
...
...
return redirect('/app/bimbingan')
else:
context={
"form": form
}
return render(request, 'app/mengelola_bimbingan/upload_bimbingan.html', context)
I already tried to use nama_pembimbing = form.cleaned_data["nama_pembimbing"]
in views.py
, but it’s still not working since the form.is_valid()
always return false, so I removed it. I use django & python for this and purpusely don’t use javascript to handle the uploaded file & selected option. I really hope that someone can help me to resolve this issue.
Answers:
I think this part of your html code is just messed up.
<select class="form-control mb-2" id="id_nama_pembimbing" name="nama_pembimbing" required>
<option value = "" selected="selected">---------</option>
<option value = {{form.nama_pembimbing}}></option>
</select>
I recommend you not to give whitespace between html attribute and its value. So, it should be like this:
<select class="form-control mb-2" id="id_nama_pembimbing" name="nama_pembimbing" required>
<option value="" selected="selected">---------</option>
<option value="{{form.nama_pembimbing}}">{{form.nama_pembimbing}}</option>
</select>
I already got a solution for this issue. The problem is on dropzone. I tried to remove dropzone and the selected value from dropdown can be retrieved in views. So, to solve this issue, I need to add the following codes inside my dropzone script.
init: function() {
dzClosure = this;
this.on("sending", function(data, xhr, formData) {
formData.append("nama_pembimbing", jQuery("#id_nama_pembimbing").val());
});
}
That’s all and it should be fuctioned well.
I’m working on an upload file feature with dropdown and dropzone. But, whenever I submit the uploaded file with selected option, it always says that the selected option is None. I found it None after I printed the nama_bimbingan in views.py. Here it is my code.
url.py
....
url(r'bimbingan/add/upload', mengelola_bimbingan.upload_data_bimbingan, name='add-bimbingan-excel'),
url(r'bimbingan/download-template/', mengelola_bimbingan.bimbingan_template, name='bimbingan-template'),
....
forms.py
class UploadBimbinganForm(forms.Form):
...
...
dropdown_choices = tuple(zip(all_pembimbing, all_pembimbing))
nama_pembimbing = forms.ChoiceField(choices = dropdown_choices)
upload_bimbingan.html
<form method="POST" action="{% url 'app:add-bimbingan-excel' %}" enctype="multipart/form-data">
{% csrf_token %}
<div class="py-3">
<label for="id_nama_pembimbing"> Nama Pembimbing Akademik: </label>
<select class="form-control mb-2" id="id_nama_pembimbing" name="nama_pembimbing" required>
<option value = "" selected="selected">---------</option>
<option value = {{form.nama_pembimbing}}></option>
</select>
</div>
<div id="myDropzone" class="dropzone" drop-zone>
<h6 class="dz-message"> Drop file here or click to upload</h6>
</div>
<div class="py-3">
<a href="{% url 'app:bimbingan-template' %}">Download Template</a><br>
<div class="row justify-content-between py-3">
<div class="col-md-5 mb-1">
<a href="{% url 'app:read-all-bimbingan' %}" class="btn btn-blue-outlined">Batal</a
</div>
<div class="col-md-5">
<input type="submit" value="Simpan" class="btn btn-block btn-blue">
</div>
</div>
</div>
</form>
views.py
@login_required(redirect_field_name='index')
@user_passes_test(only_admin_access)
def upload_data_bimbingan(request):
form = UploadBimbinganForm(request.POST or None)
if request.method == "POST" and 'file' in request.FILES:
nama_pembimbing = request.POST.get('nama_pembimbing')
excel_file = request.FILES["file"]
data = get_data(excel_file, column_limit=1)
bimbingans = data["Bimbingan"]
...
...
if(len(duplicate) == 0):
space_parsed_query = nama_pembimbing.replace(' ', '%20')
cleaned_query = space_parsed_query.replace(',', '%2C')
nip_pembimbing = int(get_data_dosen_by_nama(cleaned_query)[0]["nomor"])
for bimbingan in bimbingans:
if(bimbingan[0] != "NPM"):
new_bimbingan = Bimbingan(nip_pembimbing=nip_pembimbing, npm_mahasiswa=bimbingan[0])
new_bimbingan.save()
return redirect('/app/bimbingan')
else:
...
...
return redirect('/app/bimbingan')
else:
context={
"form": form
}
return render(request, 'app/mengelola_bimbingan/upload_bimbingan.html', context)
I already tried to use nama_pembimbing = form.cleaned_data["nama_pembimbing"]
in views.py
, but it’s still not working since the form.is_valid()
always return false, so I removed it. I use django & python for this and purpusely don’t use javascript to handle the uploaded file & selected option. I really hope that someone can help me to resolve this issue.
I think this part of your html code is just messed up.
<select class="form-control mb-2" id="id_nama_pembimbing" name="nama_pembimbing" required>
<option value = "" selected="selected">---------</option>
<option value = {{form.nama_pembimbing}}></option>
</select>
I recommend you not to give whitespace between html attribute and its value. So, it should be like this:
<select class="form-control mb-2" id="id_nama_pembimbing" name="nama_pembimbing" required>
<option value="" selected="selected">---------</option>
<option value="{{form.nama_pembimbing}}">{{form.nama_pembimbing}}</option>
</select>
I already got a solution for this issue. The problem is on dropzone. I tried to remove dropzone and the selected value from dropdown can be retrieved in views. So, to solve this issue, I need to add the following codes inside my dropzone script.
init: function() {
dzClosure = this;
this.on("sending", function(data, xhr, formData) {
formData.append("nama_pembimbing", jQuery("#id_nama_pembimbing").val());
});
}
That’s all and it should be fuctioned well.