'Django next page not working in login page

I open my browser, and try to access my Django site. I'm logged out of my account, so I get redirected to my login page, with the page I'm going to as a GET parameter.

http://website.com/login/?next=/customer/7/54

However, when I enter my login information, I get redirected to:

http://website.com/

Even though, I'm trying to get to:

http://website.com/customer/7/54

/customer/7/54 points to a view that does have the @login_required decorator, but I was under the assumption, that it'd still point to that view after I logged in.What is going on?

EDIT: So I realized, I had a hidden input tag:

    <input type="hidden" name="next" value='/'>

However, how do I pass in the GET paramter '/customer/7/54/ into the login template?



Solution 1:[1]

For people using their own view. You have to send "next" to your template

def login_view(request):
    '''..your logic..'''
    ctx = {'form':form,'next':next}
    return render_to_response('login.html',ctx,context_instance=RequestContext(request))

Solution 2:[2]

You can write your custom view function for login like this:

in views.py

def login(request):
    next = request.GET.get('next')
    if request.method == 'POST':
        ...
        # if user logged in
        if request.GET.get('next') != None:
          return redirect(request.GET.get('next')) 
        return redirect('/')
    else: # request.method == get 
        return render(request, 'login.html', {'next': next})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Claudio Roman
Solution 2 Amirhossein Rafiee