- Author:
- bkeating
- Posted:
- November 10, 2011
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
Sometimes I don't want to reveal a staff-only view so I created this decorator, using django.contrib.admin.views.decorators.staff_member_required
as my boilerplate. Non staff members are kicked to the 404 curb.
Suggestion: Create a file, decorators.py
in your project (or single app) and import like so: from myproject.app_name.decorators import staff_or_404
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | try:
from functools import wraps
except ImportError:
from django.utils.functional import wraps # Python 2.4 fallback.
from django.http import Http404
def staff_or_404(view_func):
"""
Decorator for views that checks that the user is logged in and is a staff
member, raising a 404 if necessary.
"""
def _checklogin(request, *args, **kwargs):
if request.user.is_active and request.user.is_staff:
# The user is valid. Continue to the admin page.
return view_func(request, *args, **kwargs)
else:
raise Http404
return wraps(view_func)(_checklogin)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.