[Python] request.method == POST equaling true in next function causing it to run prematurely

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    so i am running this code

    def login_or_join(request):

    if request.method == "POST":
    option = request.POST.get("option")
    print('post request recieved')

    if option == "1":
    return login_screen(request)

    if option == '2':
    return in_game(request)

    return render(request,"login_or_join.html")


    and def login_screen() looks like this

    def login_screen(request):
    if request.method == "POST":

    username = request.POST.get("username")
    password = request.POST.get("password")
    print(username)
    print(password)

    user = authenticate(request, username=username, password=password)

    print(user)

    if user is not None:
    return redirect('join_lobby')
    else:
    return render(request,'login_page.html',)

    return render(request, 'login_page.html')


    Whenever I click "option 1" it runs login_screen but in a way I don't want it to. it seems to just follow that request.method == "POST" and prints username and password immediately, meaning it is setting username and password immediately making any log in attempt wrong. But I don't want it to set those (or print them) until I've pressed the button on the next page. Further more when I hit "enter" or "log in" it doesn't reshow the page with the error message, it just goes back login_or_join(). I feel like I am taking crazy pills as I've been working on this website for a while and this is the first time I'm having this kind of issue. I've tried messing with it but feel I've been staring at it too long. Any help would be appreciated!

    Continue reading...

Compartilhe esta Página