I try to write a custom decorator in Django.
I want to redirect users to custom login page if they are not logged in via that page.
I have written the decorator and I debugged it. Although it works well if user is not logged in, after user logged in it gives error as
The view APPNAME.views.home didn't return an HttpResponse object.
You can find my code below. Why my decorator simply jump to return wraps(func)(inner_decorator) section after authentication in private alfa page ?
Thanks
def private_alfa_required():
def decorator(func):
def inner_decorator(request,*args, **kwargs):
if 'isPrivateAlfaUser' not in request.session or request.session['isPrivateAlfaUser'] != True:
return render_to_response('homepage.html')
return wraps(func)(inner_decorator)
return decorator
@private_alfa_required()
def home(request):
.....
def home2(request):
.....
Because you haven’t actually called the view function if your test succeeds.
As a secondary note, you don’t need the outer level of wrapper here because your decorator doesn’t take any arguments. If you drop that, you also need to drop the
()on the decorator itself.