Reputation: 66
I have a decorator protecting certain parts of my site.
def admin_only(view):
@wraps(view)
def _view(request, *args, **kwargs):
if not Group.objects.get(name='admin') in request.user.groups.all():
return redirect('main:unauthorized')
return view(request, *args, **kwargs)
return _view
So if the user is not in the admin group, they are redirected to a view that renders an 'unauthorized blah blah blah template'.
What I want to do is redirect the user back to where they were when they tried to access the forbidden page. I'll then use something like
if not Group.objects.get(name='admin') in request.user.groups.all():
messages.error("Unauthorized blah blah blah"
**redirect to where they were or home incase they used direct link**
Upvotes: 0
Views: 88
Reputation: 66
request.META.get('HTTP_REFERER')
is the link the user was at when they tried to access the forbidden view
def admin_only(view):
@wraps(view)
def _view(request, *args, **kwargs):
if not Group.objects.get(name='admin') in request.user.groups.all():
messages.error(request, "Access Denied")
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
return view(request, *args, **kwargs)
return _view
You can follow the conversation on Reddit
Upvotes: 0