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
Use pip: pip install django-tigger.
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 ;-)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-tigger-1.0.1.tar.gz
.
File metadata
- Download URL: django-tigger-1.0.1.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac2fd5770bda65ecb6004f3ca45bd7cc614f15d1ea60697a4628ab15792ca819 |
|
MD5 | 207510178fbe6c28b902a3f724353704 |
|
BLAKE2b-256 | b46ecd1dfebda560f86665a60f87616d09600cd8d801e806324336d345e4f205 |
File details
Details for the file django_tigger-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_tigger-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc30ff230299841b9801b2dc3d264deb8941da3e273e1956d143f08be3b34f54 |
|
MD5 | a53b6203b8f3888d849f978d1299d6c2 |
|
BLAKE2b-256 | 083d2c00907a558e8dddc832d818550f0e313db909bc67d080e7ee8afdbb686e |