Skip to main content

SweetAlert integration for Django

Project description

Sweetify - SweetAlert for Django

Build Status Latest Version Coverage Status

Sweetify allows you to use SweetAlert or SweetAlert2 for your temporary messages. See the examples below, to see how to use this library

Installation

Note: This package does not provide the client-side files of SweetAlert. You have to provide them yourself.

Install the latest version with pip:

pip install --upgrade sweetify

Then you have to add sweetify to your django apps:

INSTALLED_APPS = [
    ...
    'sweetify'
]

Next up you have to specify, in your settings, which library you are using (SweetAlert or SweetAlert2):

# possible options: 'sweetalert', 'sweetalert2' - default is 'sweetalert2'
SWEETIFY_SWEETALERT_LIBRARY = 'sweetalert2'

Next add the following lines to the bottom of your layout/base template:

...

{% load sweetify %}
{% sweetify %}

</body>
</html>

Usage

You can now easily create alerts in your views with any of the following methods provided by Sweetify:

import sweetify

# Base method with no type specified
sweetify.sweetalert(self.request, 'Westworld is awesome', text='Really... if you have the chance - watch it!', persistent='I agree!')

# Additional methods with the type already defined
sweetify.info(self.request, 'Message sent', button='Ok', timer=3000)
sweetify.success(self.request, 'You successfully changed your password')
sweetify.error(self.request, 'Some error happened here - reload the site', persistent=':(')
sweetify.warning(self.request, 'This is a warning... I guess')

We also support toast messages (SweetAlert2 only)

import sweetify

# Base method, default icon is set to success
sweetify.toast(self.request, 'Cheers to new toast')

sweetify.toast(self.request, 'Oops, something went wrong !', icon="error", timer=3000)
sweetify.toast(self.request, 'Persistent toast that only goes away once clicked', icon='warning', persistent="Bye toast!")

Additionally, you can issue multiple alerts without reloading the page ONLY if you are using SweetAlerts 2. To do so, you must define your options in a dictionary:

import sweetify

# Call two consecutive alerts (args1 is the options dict for the first alert and args2 the one for the second alert):
sweetify.multiple(self.request, args1, args2)

# Call five consecutive alerts:
sweetify.multiple(self.request, args1, args2, args3, args4, args5)

Example Usage

import sweetify

def test_view(request):
    sweetify.success(request, 'You did it', text='Good job! You successfully showed a SweetAlert message', persistent='Hell yeah')
    return redirect('/')

Example usage for multiple alerts:

import sweetify

def test_view(request):
    args1 = dict(title='Test1', icon='info', text="Text placeholder1", button="Next")
    args2 = dict(title='Test2', icon='success', text="Text placeholder2", timer=5000, timerProgressBar='true', persistent="Close")
    sweetify.multiple(request, args1, args2)
    return redirect('/')

Replacement for SuccessMessageMixin

Sweetify includes a drop-in replacement for SuccessMessageMixin. Just replace the Django mixin with Sweetify's SweetifySuccessMixin and you are good to go.

from sweetify.views import SweetifySuccessMixin

class TestUpdateView(SweetifySuccessMixin, UpdateView):
    model = TestModel
    fields = ['text']
    success_message = 'TestModel successfully updated!'

Options

By default, all alerts will dismiss after a sensible default number of seconds.

Default options set by Sweetify:

sweetify.DEFAULT_OPTS = {
    'showConfirmButton': False,
    'timer': 2500,
    'allowOutsideClick': True,
    'confirmButtonText': 'OK',
}

The following special options provided by Sweetify are available:

# Shows the alert with a button, but will still close automatically
sweetify.sweetalert(self.request, 'Title', button=True)
sweetify.sweetalert(self.request, 'Title', button='Awesome!') # Custom text for the button

# Shows the alert with a button and only closes if the button is pressed
sweetify.sweetalert(self.request, 'Title', persistent=True)
sweetify.sweetalert(self.request, 'Title', persistent='Awesome!') # Custom text for the button

You also can use any other available option that SweetAlert accepts:

sweetify.info(self.request, 'Sweet!', text='Here is a custom image', imageUrl='images/thumbs-up.jpg', timer=5000)

If you use CSP you can use the optional nonce parameter on the sweetify tag:

{% load sweetify %}

<!-- static nonce -->
{% sweetify nonce="XYZ" %}

<!-- from request -->
{% sweetify nonce=request.csp_nonce %}

Development

Use the Makefileto execute common tasks:

  • Install dependencies
$ make install
  • Run all tests
$ make test

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sweetify-2.3.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

sweetify-2.3.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file sweetify-2.3.1.tar.gz.

File metadata

  • Download URL: sweetify-2.3.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Windows/10

File hashes

Hashes for sweetify-2.3.1.tar.gz
Algorithm Hash digest
SHA256 37100d8f02929076cd060b1c1aae7575448af32e61ca8c03bc003548e31ffc70
MD5 12e5bd5cc6d668b4108c1d28f5f143bb
BLAKE2b-256 aa6158e5088c21f01d5bb4386fc056db0e0fb2f33179e92022cb7a1f6afb0fd1

See more details on using hashes here.

File details

Details for the file sweetify-2.3.1-py3-none-any.whl.

File metadata

  • Download URL: sweetify-2.3.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Windows/10

File hashes

Hashes for sweetify-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 941ed8914467d5a29eb263079fd4f39661df81bfded6476ed4ef2ad57a4c25ba
MD5 475a6c2fb29ff57806a578f10f922481
BLAKE2b-256 dd2083496b38a1b995b41c69c12b9287d4b6e4fbf83882261a615d9a5ddf7420

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page