简体   繁体   中英

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

 <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

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

i dont know why form.is_valid returning false and even the error is not interpreted to me!! 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 .

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

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM