Search code examples
djangodjango-templatesalertdjango-class-based-viewssweetalert2

Django - Why Sweetify is not working. SweetAlert2


I'm followed the documentation from Atrox, and then still not working on my project. Any Idea ?

I write the code like below.

1. home/views.py

from django.shortcuts import redirect
from django.views.generic import FormView
from attendance.models import Graduates
from home.forms import TicketForm
from django.utils import timezone
from django.urls import reverse
import sweetify

class HomeView(FormView):
template_name = 'home/index.html'
form_class = TicketForm

def form_valid(self, form):
    getId = form.cleaned_data['nomor_ticket']      
    
    try:
        graduate = Graduates.objects.get(ticket_id=getId)
        message_success = 'Congratulations'

        if graduate:
            graduate.attendance = timezone.now()
            graduate.status = True
            graduate.save()
            sweetify.success(self.request, message_success, text='Cool', persistent='Thanks', timer='3000')
            return (redirect('attendance:index', graduate.ticket_id))

    except Graduates.DoesNotExist:
        print('ticket not found')
        return redirect('home:index')

2. templates/base.html

...
<body>
  {% block contents %}{% endblock contents %}
  <script src="https://unpkg.com/[email protected]/dist/flowbite.js"></script>
  {% load sweetify %}
  {% sweetify %}
</body>
...

3. core/settings.py

INSTALLED_APPS = [
...
'sweetify'
...
]

SWEETIFY_SWEETALERT_LIBRARY = 'sweetalert2'

Solution

  • Here done with this code

    ------- base.html ---------

    {% load static %}
    
     <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>
            {% block title %}
                                          
            {% endblock title %}
        </title>
    </head>
    <body>
      
      {% block body %}
       
      {% endblock body %}
      <script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/11.6.5/sweetalert2.all.min.js"></script>
      
    
    </body>
    </html>
    
    
    {% load sweetify %}
    {% sweetify %}
    

    Output with bowser

    enter image description here