Skip to content
SweetAlert integration for Django
Branch: master
Clone or download
Latest commit e785a7f Apr 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
sweetify release v2.0.0 Apr 22, 2019
tests update test settings to use new sweetify setting Apr 22, 2019
.gitattributes initial Sep 6, 2017
.gitignore Improve suite tests for receive new contributions (#4) Dec 6, 2018
.travis.yml
LICENSE initial Sep 6, 2017
Makefile Improve suite tests for receive new contributions (#4) Dec 6, 2018
README.md update readme Apr 22, 2019
poetry.lock update deps Apr 22, 2019
pyproject.toml release v2.0.0 Apr 22, 2019

README.md

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')

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('/')

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)

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:

You can’t perform that action at this time.