Skip to main content

Bounce users out of your view by raising an exception

Project description

Bounce users out of your view by raising an exception

Introduction

You’re probably used to this sort of workflow:

from django.http import Http404
from django.views.generic import TemplateView

class MyView(TemplateView):
    def get_context(self, **kwargs):
        # do some stuff
        raise Http404

…but wouldn’t it be nice if rather than just bombing out with a 404, you could bounce the user out to a redirect and attach a friendly message? That’s what django-tigger does!

from django.core.urlresolvers import reverse
from django.views.generic import TemplateView
from tigger.exceptions import Bounce

class MyView(TemplateView):
    def get_context(self, **kwargs):
        # do some stuff
        raise Bounce(
            reverse("some-url"),
            message="The wonderful thing about tiggers...",
            level="WARN"
        )

The above code will bounce the user to whatever some-url resolves to and dump a message on the screen using the Django messaging framework.

Installation

  1. Use pip: pip install django-tigger.

  2. Add django_tigger.middleware.BouncingMiddleware to your MIDDLEWARE list in your Django settings. Note that you don’t need to add this to INSTALLED_APPS as this package doesn’t have any models, migrations, or anything that would require that.

Use

Basically anywhere you want to just bail out of your current process and instead redirect the user, call Bounce() and pass in a few arguments:

  • url: The only required argument, this is where you’re bouncing your user to.

  • message: If supplied, this will attach a message to the user’s session using the Django messaging framework

  • level: The level of the message. Must be one of DEBUG, INFO, SUCCESS, WARNING, ERROR. The default is INFO.

Support

I’ve used this in Python 3 and Django 1.11, but I see no reason why it wouldn’t work in Python 2.7 and Django 1.10. Older than that though, and you’ll have trouble. Newer than that, and you should be fine.

This isn’t particularly complex code ;-)

Colophon

The wonderful thing about tiggers
Is tiggers are wonderful things
Their tops are made out of rubber
Their bottoms are made out of springs
They're bouncy, trouncy, flouncy, pouncy fun, fun, fun, fun, fun
But the most wonderful thing about tiggers is I'm the only one
I'm the only one.
-- Winnie the Pooh and Tigger Too!, 1974

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

django-tigger-1.0.2.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

django_tigger-1.0.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file django-tigger-1.0.2.tar.gz.

File metadata

File hashes

Hashes for django-tigger-1.0.2.tar.gz
Algorithm Hash digest
SHA256 632f8164646c311d1722b00bd58e7dd7f92465c398e45df95b81d51f3755567a
MD5 aca69030a03ad7707476653687fa3a59
BLAKE2b-256 45651c25c737c912749efaca7c5c89ce7907b08c0213af5f41592711c426ab6b

See more details on using hashes here.

File details

Details for the file django_tigger-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_tigger-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b39d5fc3c21e0754320f1b88f1455fd43b5eb06fd2a0ba1f44a4009e24c78b47
MD5 44c6d5650b1521053e70b97229d6f0f6
BLAKE2b-256 79217c442fc61b1678a0e8db23b8d9c684d86d913de4fc8d704026a8792055d9

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