jwad yari
jwad yari

Reputation: 11

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

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 in my terminal:

<QueryDict: {'csrfmiddlewaretoken': ['rxjKh3oNsgdRBtT20rT1uVWLw2qOR2oQqjOct9iPhEsnJiHAUVgriiDj3Tg1qoog']}>

  • username
  • This field is required.
  • password
  • This field is required.
  • and this in my browser: form is not valid

    update: this is for my main page :

               {% csrf_token %}
               <button type="submit">ورود (Login)</button>
           </form>
           <form action="/signup/" method="post">
               {% csrf_token %}
               <button type="submit">ثبت نام (Sign Up)</button>
           </form>
    

    this is my whole login html:

     <!DOCTYPE html>
    <html lang="fa">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>صفحه ورود</title>
    </head>
    <body>
        <div class="login-container">
            <h2>ورود به حساب کاربری</h2>
            <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>
        </div>
    </body>
    </html>
    

    second update : my account app urls.py

    from django.urls import path
    from accounts.views import *
    
    urlpatterns = [
        path("", home_view, name='home'),
        path("login/", login_view , name='login'),
        path("signup/", signup_view , name='signup')
    ]
    

    my project urls.py:

    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('',include('accounts.urls')),
        path('transitions/',include("transitions.urls"))
    ]
    

    Upvotes: 1

    Views: 76

    Answers (0)

    Related Questions