Django sweetify

Viewed 1497

I already read the documentation about https://github.com/Atrox/sweetify-django but I don't understand clearly, I already download and import requirement of sweetify in Django. I just want that if the record is updated the popup message (sweetify) will appear.

def studentrecords(request):
   if request.method == 'POST':
      id = request.POST.get("id")
      update = StudentsEnrollmentRecord.objects.get(id=id)
      update.Section = s
      update.save()
      sweetify.success(request, 'You did it', text='Your Form has been Updated',persistent='Hell yeah')
      return render(request, 'Homepage/selectrecord.html')

this is my html

{% load sweetify %}
{% sweetify %}
 <form  method="post" action="/studentrecords/" enctype="multipart/form-data">{% csrf_token %}
<table>
    {% for student in myrecord %}
    <tr>
        <td>Control #</td>
        <td><input type="text" name="id" value="{{student.id}}"></td>
        <td><input type="submit"></td>
    </tr>
    <tr>
        <td>Name: </td>
        <td><input type="text" value="{{student.Student_Users.Firstname}} {{student.Student_Users.Lastname}} {{student.Student_Users.Middle_Initial}}"></td>
        <td>Course/Track</td>
        <td><input type="text" value="{{student.Courses}}"></td>
    </tr>
    <tr>
        <td>Education Level: </td>
        <td><input type="text" value="{{student.Education_Levels}}"></td>
        <td>Strand: </td>
        <td><input type="text" value="{{student.strands}}"></td>
    </tr>
    <tr>
        <td>Section: </td>
        <td>
            <select name="section">
                <option value="{{student.Section.id}}">{{student.Section}}</option>
                {% for sections in section %}
                <option value="{{sections.id}}">{{sections.Description}}</option>
                {% endfor %}
            </select>
        </td>
        <td>Payment Type: </td>
        <td><input type="text" value="{{student.Payment_Type}}" class="myform"></td>
    </tr>
    {% endfor %}
</table>
 </form>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script>
    Swal.fire(
  'Good job!',
   'Data Updated!',
  'success'
)
</script>

my settings.py

INSTALLED_APPS = [
    #my apps
    ….
    'sweetify'
]
SWEETIFY_SWEETALERT_LIBRARY = 'sweetalert2'

I didn't receive any error , but no popup message appear (sweetify)

2 Answers

I think it might just be that you just need to load the script over CDN at the top of the page. Looking at the library, it seems like the template tag {% sweetify %} just loads in something a bit like this:

<script>
    Swal.fire(...)
</script>

So if you haven't yet loaded the sweet alerts script, Swal will just be undefined. You'll be able to confirm if this is the problem by opening up your dev-tools, and in the console you'll see a message which is something like "undefined has no property fire" or something like that. I'm curious, does the Swal.fire you have at the bottom of the page work?

First off, if you'd like to get a notification on any record update, I'd suggest using Signals rather than detecting the request method. It's a way more central and robust solution. Just add this to your models.py:

from django.db.models.signals import post_save

@receiver(models.signals.post_save, sender=StudentsEnrollmentRecord)
def update_enrollment_record_handler(sender, updated_instance, **kwargs):
    """This method is called after every call of StudentsEnrollmentRecord.save"""
    sweetify.success(request, 'You did it', text='Your Form has been Updated',persistent='Hell yeah')

Second - POST is meant to create an object through a RestAPI, you should switch to UPDATE (includes all objects fields) or PUT (updates some of the objects fields).

Third - what happens when you add a print statement above the sweetify line? Does that print run? If not, then your whole setup doesn't even call the studentrecords method and you'll need to share more info so we can replicate the situation and/or advise further.

Related