When using Django with a Bootstrap Modal to submit a radio button choice, the modal does not want to close with the submission. It proceeds with the submission to the view in views.py but does not close.
When changing the name so that it does not go to the view the modal does close.
I have tried many of the suggestions from other posts including this one.
main html
<!DOCTYPE html>
{% load static %}
<html>
<body>
<div id="spacer">
</div>
<div style="padding-left:20px;">
<button type = "button" class="btn" data-toggle="modal" data-
target="#filetypeModal">Get Device Data</button>
</div>
<!-- File Type Modal -->
<div class="modal fade" id="filetypeModal" tabindex="-1" role="dialog" aria-labelledby="filetypeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<form action="" method="post" id="getAPI">
{% csrf_token %}
<div class="modal-header" style="background-color: darkblue; color: white">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="color: white">×</button>
<h4 class="modal-title" id="filetypeModalLabel">File Types</h4>
</div>
<div class="modal-body" style="text-align: center;">
Choose file type to download.<br>
<br>
<label class="radio-inline"><input type="radio" name="choices" value="Excel" checked>Excel</label>
<label class="radio-inline"><input type="radio" name="choices" value="CSV">CSV</label>
</div>
<div class="modal-footer" style="background-color: darkblue;">
<button type="submit" class="btn btn-primary" value="OK" name="getAPIsubmit">OK</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js">
views.py
def tasks(request):
if request.POST.get('getAPIsubmit'):
request.session['choice'] = request.POST.get("choices")
response = INDget.indget(request)
return response
return render(request, 'tasks.html')
def Download_file(request, fname):
import ntpath
from wsgiref.util import FileWrapper
wrapper = FileWrapper(open(fname, 'rb'))
response = HttpResponse(wrapper, content_type = 'application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=' + ntpath.basename(fname)
return response
Solution...
added to views.py
def reload_tasks(request):
return render(request, 'tasks.html')
At the end of my API script...
from main.views import Download_file
from main.views import reload_tasks
choice = request.session['choice']
if choice == "Excel": Download_file(request, './INDmain/IND_data.xlsx')
if choice == "CSV": Download_file(request, './INDmain/IND_data.csv')
return reload_tasks(request)