Skip to main content

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="SUCCESS"
        )

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

Metadata

Release files for django-tigger 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-tigger 1.1.0
File Size Uploaded
django-tigger-1.1.0.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-tigger 1.1.0
File Interpreter ABI Platform
django_tigger-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.1 kB

Release files / django-tigger-1.1.0.tar.gz

Download URL django-tigger-1.1.0.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6aaab4cde698efdbf992fdb14e8e4944a1b45202b1d4d73ba096619499c58f10
BLAKE2b-256 checksum
How to use checksums
12f9023b9b46be28c6ba9b0dfd644d678940648e8c3ebaf63bc931d1ee9a77be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_tigger-1.1.0-py3-none-any.whl

Download URL django_tigger-1.1.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5bec7d21e9081ff0fc200bdd99b340b84d20b4df401ff5a2990a762542b9eaae
BLAKE2b-256 checksum
How to use checksums
c7df0faf797f42be5f6fe057c85997b9ea6584e90fab658c447c0bbceea81d3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page