表单对象在django中没有属性'isValid'

Hem*_*S R 0 python django django-templates django-forms django-views

如果我打印request.POST.usernamerequest.POST.password,我得到正确的数据.但我无法验证表格.我无法得到cleaned_data.

views.py

def login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.isValid():
            print "coming"
        return render_to_response('html/index.html')
    else:
        form = LoginForm()
        c = {'logInForm': form, }
        return render_to_response('html/index.html', c, RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

forms.py

from django import forms
class LoginForm(forms.Form):
    username = forms.EmailField()
    password = forms.CharField(max_length=50)
Run Code Online (Sandbox Code Playgroud)

的index.html

<!DOCTYPE html>
<html>
<head>
    .....
</head>
<body>

<div class="container">

    <form class="form-signin" action="login" method="post">{% csrf_token %}
        {{ logInForm.as_p }}
        <input type="submit" value="Submit"/>
    </form>
</div> 
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

JcK*_*ley 7

您没有正确使用该方法.正确的方法是is_valid

Django Docs

def login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid(): # <<<< Correct!
            print "coming"
        return render_to_response('html/index.html')
Run Code Online (Sandbox Code Playgroud)