Hello everyone,
For a Django project, I have a page that allows to load an XLS file and generate an HTML table that appears on the page, as well as an HttpResponse object of the same file in XLSX format.
I manage to display my HTML table but I would like that, with a “import” button, we can import the file in HttpResponse.
My problem is that when I put my “Import” button in a form type=POST, it refreshes the page and my html table disappears as well as the file inside the input button.
I thought of importing it by a simple button with Javascript but I can’t…
Do you have any ideas on how I could successfully import my file without refreshing the page? Or maybe how can I improve my idea to make it easier to do?
Thanks in advance!
template.html
<form method='POST' enctype="multipart/form-data">{% csrf_token %}
<input type='file' name='InputFile' accept='.xls'>
</form>
<div id=table>
{{ table }}
</div>
<form method='POST' enctype="multipart/form-data">{% csrf_token %}
<input type='submit' name='importXLSXFile' value='{{ myHttpResponse }}'>
</form>
views.py
def main(request):
if request.method == 'POST' and request.FILES.get('InputFile') != None:
#some_scripts
return render(request, 'template.html',{'table':table,'myHttpResponse ': HttpResponse })
if request.method == 'POST' and "importXLSXFile" in request.POST :
#i_dont_know_what_to_do_here