Hello everyone,
I want to show an error message with sweetalert, if it has the same project name. The code works in message.success state but not in message.error state and django’s yellow error page is displayed on the screen. In case of error, I want my own sweetalert instead of yellow error page. What should I do? Thank you for the help in advance.
views.py
def addproject(request):
if request.method == "POST":
if request.POST.get('project_name') \
and request.POST.get('prefix') \
and request.POST.get('project_description'):
# do not let add row if project is already identified
if request.POST.get('project_name') not in Projects.objects.values('name'):
project = Projects()
project.name = request.POST.get('project_name')
project.prefix = request.POST.get('prefix')
project.description = request.POST.get('project_description')
project.save()
messages.success(request, "New project data is added successfully!")
else:
messages.error(request, "You already have that project sample!")
return HttpResponseRedirect("/")
else:
return render(request, 'add.html')
home.html
{% for message in messages %}
{% if message.tags == 'success' %}
<!-- sweetalert js -->
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
var m = "{{ message }}";
Swal.fire({
icon: 'success',
iconHtml: '<i class="fa fa-check"></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>
{% else %}
<!-- sweetalert js -->
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
var m = "{{ message }}";
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 %}
{% endfor %}