Django客户端登录重定向循环

时间:2017-01-18 19:16:31

标签: python django redirect

django的新手,并尝试解决公司门户网站当前的问题。使用人员帐户登录站点时,登录正常,但使用客户帐户返回ERR :: TOO_MANY_REDIRECTS。

def user_login(request):
# Like before, obtain the context for the user's request.
context = RequestContext(request)
form = AuthenticationForm()
# If the request is a HTTP POST, try to pull out the relevant information.
if request.method == 'POST':
    # Gather the username and password provided by the user.
    # This information is obtained from the login form.
    username = request.POST['username']
    password = request.POST['password']

    # Use Django's machinery to attempt to see if the username/password
    # combination is valid - a User object is returned if it is.
    try:
        user = authenticate(username=username, password=password)
    except LockedOut:
        messages.error(request, 'You have been locked out because of too many login attempts. Please try again in 10 minutes.')

    # If we have a User object, the details are correct.
    # If None (Python's way of representing the absence of a value), no user
    # with matching credentials was found.
    else:
        if user:
            # Is the account active? It could have been disabled.
            if user.is_active:
                # If the account is valid and active, we can log the user in.
                # We'll send the user back to the homepage.
                login(request, user)
                if request.user.is_client:
                    messages.error(request, 'Something is not working correctly.')
                elif request.user.is_staff:
                    return redirect('home')

            else:
                # An inactive account was used - no logging in!
                messages.error(request, 'Your account is disabled.')

        else:
            messages.error(request, 'The credentials you entered are invalid.')
# Bad login details were provided. So we can't log the user in.
# The request is not a HTTP POST, so display the login form.
# This scenario would most likely be a HTTP GET.
# No context variables to pass to the template system, hence the
# blank dictionary object...
#return render_to_response('administrative/login.html', {'form': form}, context)
return redirect('administrative/login.html')

当使用员工帐户运行此代码时,redirect('home')工作正常,但客户帐户甚至不会运行错误消息,他们只是立即重定向。

1 个答案:

答案 0 :(得分:0)

您重定向到也会重定向的网页,很可能是因为用户尚未登录。