在django中登录时出现语法错误

时间:2015-08-06 08:45:54

标签: python django

我在最后一个else语句中遇到语法错误,我不知道我做得不对,但假设它是一个标识错误的结果。崇高的文字自动识别没有帮助我:

@csrf_exempt
def pagelogin(request):
    context =RequestContext(request)
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        if user is not None:
            # 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)
                #return HttpResponseRedirect('/home/')
                return render(request,'index.html')
            else:
                # An inactive account was used - no logging in!
                return HttpResponse("Your  account is disabled.")
        else:
            # Bad login details were provided. So we can't log the user in.
            print "Invalid login details: {0}, {1}".format(username, password)
            return HttpResponse("Invalid login details supplied.")

    # The request is not a HTTP POST, so display the login form.
    # This scenario would most likely be a HTTP GET.
    else:
        # No context variables to pass to the template system, hence the
        # blank dictionary object...
        #messages.success(request, 'You have successfully logged in to your account')
        return render(request,'pagelogin.html')

1 个答案:

答案 0 :(得分:0)

像这样缩进:

@csrf_exempt
def pagelogin(request):

    context =RequestContext(request)

    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(username=username, password=password)
        if user is not None:
                # 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)

                    #return HttpResponseRedirect('/home/')

                    return render(request,'index.html')
                else:
                    # An inactive account was used - no logging in!
                    return HttpResponse("Your  account is disabled.")
        else:
            # Bad login details were provided. So we can't log the user in.
            print "Invalid login details: {0}, {1}".format(username, password)
            return HttpResponse("Invalid login details supplied.")

    # The request is not a HTTP POST, so display the login form.
    # This scenario would most likely be a HTTP GET.
    else:
        # No context variables to pass to the template system, hence the
        # blank dictionary object...
        #messages.success(request, 'You have successfully logged in to your account')
        return render(request,'pagelogin.html')