KSHMR
KSHMR

Reputation: 809

Django - Simple redirect to requested URL

I'd like to do that, but this

return HttpResponseRedirect(request.path_info)

yields a redirect loop. I suspect it's because the request object is the website you're on at that time.

Whole function

def permit(request, pk):
    if int(request.user.id) == int(pk):
        return HttpResponseRedirect(request.path_info)
    else:
        return render_to_response('forbidden.html')

The user has clicked a link on a webpage. pk is the regx search term in urls.py. I want this to redirect to the page the user clicked on if the user is authorized, not the page he is currently on.

Upvotes: 0

Views: 521

Answers (1)

cchristelis
cchristelis

Reputation: 2015

The problem is that as soon as the user is "redirected", the function permit is called. The if statement is true (since it is the authenticated user) and the redirect happens again... To prevent this loop, only redirect, if there was a post request:

def permit(request, pk):
    if request.POST:
        if  int(request.user.id) == int(pk):
            return HttpResponseRedirect(request.path_info)
        else:
            return render_to_response('forbidden.html')
    ## Return your login page here.

Upvotes: 1

Related Questions