简体   繁体   English

为什么 form.is_valid 返回 false?出现此错误?

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

what is 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 file 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>

View.py file查看.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 forms.py

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

i dont know why form.is_valid returning false and even the error is not interpreted to me!!我不知道为什么 form.is_valid 返回 false 甚至错误也没有被解释给我!! what it is actually telling!它实际上在说什么!

Thanks in advance提前致谢

Rather than using u and p in name of the input fields, you should use username and password .而不是在输入字段的名称中使用up ,您应该使用usernamepassword

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

Also, update the view to get data from username and password :此外,更新视图以从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')

Finally, as you are working with django forms, why not use it in template?最后,当您使用 django forms 时,为什么不在模板中使用它呢?

To do that, you need to send the form data to template, like this:为此,您需要将表单数据发送到模板,如下所示:

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})

And use it template:并使用它模板:

<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>

Your view is incorrect.你的观点是错误的。 You need to authenticate user first then after if the user credentials are right then you can redirect to some url like this.您需要先对用户进行身份验证,然后如果用户凭据正确,您可以像这样重定向到一些 url。

Change your view like this.像这样改变你的看法。

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