Python Django request.GET.get('usernname')总是返回'None'。

问题描述 投票:0回答:1

视图.py

def login(request):
    context = {
        "login_view": "active"
    }
    if request.method == "POST":
        username = request.GET.get('username')
        password = request.GET.get('password')
        print(f"username  ===== {username}")
        print(f"password  ===== {password}")

        user = auth.authenticate(username = username, password = password)
        if user is not None:
            auth.login(request, user)
            messages.success("you are successfully logged in")
            return redirect('dashboard')
        else:
            messages.error(request, "Invalid credentials")
            return redirect('login')
    else:
        return render(request, 'accounts/login.html', context)
django post request
1个回答
1
投票

你需要将GET改为POST。例子......你需要把GET改成POST。

if request.method == "POST":
    username = request.POST['username']
    password = request.POST['password']

    user = authenticate(request, username=username, password=password)
    if user is not None:
        login(request, user)
        # Redirect to a success page.
        ...
    else:
        # Return an 'invalid login' error message.
        ...
© www.soinside.com 2019 - 2024. All rights reserved.