Hello everyone,
When I select the file and import it, if there is an error, it gives a warning with sweetalert. Afterwards, when I press the ok button and close the warning, the home.html page refreshes, but only the import part appears on the screen. Do you have any idea how to fix it? Thanks in advance.
before
after
views.py
def home(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
file_path = os.path.join(BASE_DIR, newdoc.docfile.url)
error = save_new_sample_from_csv(file_path)
# return HttpResponseRedirect("/")
return render(request, 'home.html', {'errors': str(error)})
else:
form = UploadFileForm() # A empty, unbound form
return render(request, 'home.html', { 'form': form})
home.html
<form action="{% url 'home' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="container">
<div class="row justify-content-md-center">
<div class="col col-lg-2">
{{ form.as_p }}
</div>
<div class="col col-lg-2" style="margin-left: 10px;">
<input type="submit" value="Import" />
</div>
</div>
</div>
</form>
home.html -script
{% if errors != 'None' and errors %}
<!-- sweetalert js -->
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
var m = "Cannot import the csv file. Please refresh the page.";
Swal.fire({ icon: 'error', iconHtml: '<i class="fa fa-warning"></i>', text: m, confirmButtonColor: '#fff', color: '#000', background: 'rgba(0, 0, 0, 0.7)', allowEscapeKey: false,
showClass: {
popup: 'my-icon' // disable popup animation css
},
});
</script>
{% endif %}