簡體   English   中英

為什么 form.is_valid 返回 false?出現此錯誤?

[英]why is form.is_valid returns false?with this error?

這是什么錯誤???

 <ul class="errorlist"><li>username<ul class="errorlist"><li>This field is required.</li></ul></li><li>password<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

html文件

 <form name="form" action="/accounts/logincheck" method="POST" > {% csrf_token %} <div class="container"> <div class="login"> <h1>Login to your account</h1> <input type="text" name="u" placeholder="Username" required="required"/> <input type="password" name="p" placeholder="Password" required="required"/> <button type="submit" class="btn btn-primary btn-block btn-large">Login</button> </div> </div> </form>

查看.py文件

 def logincheck(request): if request.method == "POST": MyLoginForm = LoginForm(request.POST) if MyLoginForm.is_valid(): username = MyLoginForm.cleaned_data['u'] password = MyLoginForm.cleaned_data['p'] print("Username: "+username) print("Password: "+password) response = redirect("/login") else: print(MyLoginForm.errors) response = redirect("/logout") else: response = redirect("/profile") return response

forms.py

 from django import forms class LoginForm(forms.Form): username = forms.CharField(max_length=100) password = forms.CharField(widget=forms.PasswordInput())

我不知道為什么 form.is_valid 返回 false 甚至錯誤也沒有被解釋給我!! 它實際上在說什么!

提前致謝

而不是在輸入字段的名稱中使用up ,您應該使用usernamepassword

<input type="text" name="username" id="username" placeholder="Username" required="required"/>
<input type="password" name="password" id="username" placeholder="Password" required="required"/>

此外,更新視圖以從usernamepassword獲取數據:

def logincheck(request):
    if request.method == "POST":
        my_login_form = LoginForm(request.POST)

        if my_login_form.is_valid():
            username = my_login_form.cleaned_data['username']
            password = my_login_form.cleaned_data['password']
            # authentication should be done here. documentation: https://docs.djangoproject.com/en/2.2/topics/auth/default/
            return redirect('/profile')

最后,當您使用 django forms 時,為什么不在模板中使用它呢?

為此,您需要將表單數據發送到模板,如下所示:

def logincheck(request):
    my_login_form = LoginForm(request.POST or None)
    if request.method == "POST":
        if my_login_form.is_valid():
            username = my_login_form.cleaned_data['username']
            password = my_login_form.cleaned_data['password']
            # authentication should be done here. documentation: https://docs.djangoproject.com/en/2.2/topics/auth/default/
            return redirect('/profile')
     return render('logincheck_template.html', context={'form':my_login_form})

並使用它模板:

<div class="login">
    <h1>Login to your account</h1>
       {% for field in form %}
           {{ field.errors }}
           {{ field.label_tag }} {{ field }}
       {% endfor %}
        <button type="submit" class="btn btn-primary btn-block btn-large">Login</button>
 </div>

你的觀點是錯誤的。 您需要先對用戶進行身份驗證,然后如果用戶憑據正確,您可以像這樣重定向到一些 url。

像這樣改變你的看法。

def logincheck(request):
     if request.method == "POST":
        MyLoginForm = LoginForm(request.POST)

        if MyLoginForm.is_valid():
            username = MyLoginForm.cleaned_data['u']
            password = MyLoginForm.cleaned_data['p']
            print("Username : ",username)
            print("Password : ",password)
            user = authenticate(request,username=u,password=p)
            if user is not None:
                return redirect("some-path")
     else:
             MyLoginForm = LoginForm()
     return render(reuqest,'your_login_template',{'form': MyLoginForm})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM