[Python] why request.post only send csrftoken, not my form's field

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    im new in django and this is my first project. I create a loginform and when I want to use it, It just send csrftoken not my requierd fields

    this my form:

    class MyLoginForm(forms.Form):
    username = forms.CharField(max_length=50)
    password = forms.CharField(widget=forms.PasswordInput())



    and this is my views:

    def login_view(request):
    if not request.user.is_authenticated:
    if request.method == "POST":
    form = MyLoginForm (request.POST)
    print(request.POST)
    if form.is_valid():
    print('valid!!!')
    data = form.cleaned_data
    username = data["username"]
    password = data["password"]
    try:
    account = Account.objects.get(username=username)
    except Exception as error:
    print(error)
    return HttpResponseRedirect(reverse("login"))

    if account is not None:
    if password == account.password:
    login(request, account)
    return render(request, "home.html", {"account": account})
    else:
    return HttpResponseRedirect(reverse("login"))
    else:
    return HttpResponse(f"<h2>No such a User : {username}</h2>")
    else:
    print(form.errors) # Print form errors to debug
    return HttpResponse("form is not valid")
    else:
    form = MyLoginForm(request.POST)
    return render(request, template_name="login.html", context={"form": form})
    else:
    return HttpResponseRedirect(reverse("home"))


    and this is my html:

    <form method="post" action = "">
    {% csrf_token %}
    <div class="form-group">
    <label for="username">نام کاربری</label>
    <input type="text" id="username" name="username" required>
    </div>
    <div class="form-group">
    <label for="password">کلمه عبور</label>
    <input type="password" id="password" name="password" required>
    </div>
    <button type="submit">ورود</button>
    </form>


    and Im getting this: <QueryDict: {'csrfmiddlewaretoken': ['rxjKh3oNsgdRBtT20rT1uVWLw2qOR2oQqjOct9iPhEsnJiHAUVgriiDj3Tg1qoog']}>

    • username
    • This field is required.
    • password
    • This field is required.

    Continue reading...

Compartilhe esta Página