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

如果我打印request.POST.usernamerequest.POST.password,我正在获取正确的数据。但我无法验证表单。我无法获得cleaned_data。表单对象在django中没有属性'isValid'

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

forms.py

from django import forms 

class LoginForm(forms.Form):

username = forms.EmailField()

password = forms.CharField(max_length=50)

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

回答:

您没有正确使用该方法。正确的方法是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')

回答:

适当方法名我s is_valid

以上是 表单对象在django中没有属性'isValid' 的全部内容, 来源链接: utcz.com/qa/258629.html

回到顶部