Answer the question
In order to leave comments, you need to log in
Why does a redirect work in a "pure" form, but does not work from a function?
Perhaps this is a very simple, but not yet obvious thing for me. Why does this code work...
@login_required
def mem_area(request):
""" Returns Member Area HTML with data """
user = request.user
if user.is_staff:
return redirect('/')
user_info = UserInfo.objects.get(user_id=user.id)
page_title = 'Dashboard'
return render(request, 'user_resume/member_area/member_area.html', locals())
def admin_redirect(user):
""" Checks if user is admin and don't let him open member area """
if user.is_staff:
return redirect('/')
@login_required
def mem_area(request):
""" Returns Member Area HTML with data """
user = request.user
admin_redirect(user)
user_info = UserInfo.objects.get(user_id=user.id)
page_title = 'Dashboard'
return render(request, 'user_resume/member_area/member_area.html', locals())
Answer the question
In order to leave comments, you need to log in
Because the redirect object is returned to the scope of your view, but the view itself does not return it.
The exception aborts all functions and returns 404
def admin_redirect(user):
""" Checks if user is admin and don't let him open member area """
if user.is_staff:
return redirect('/')
@login_required
def mem_area(request):
""" Returns Member Area HTML with data """
user = request.user
redirect = admin_redirect(user)
if redirect:
return redirect
user_info = UserInfo.objects.get(user_id=user.id)
page_title = 'Dashboard'
return render(request, 'user_resume/member_area/member_area.html', locals())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question