【问题标题】:form object has no attribute 'isValid' in django表单对象在 Django 中没有属性“isValid”
【发布时间】:2013-07-07 15:46:13
【问题描述】:

如果我打印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))

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>

【问题讨论】:

标签: python django django-forms django-templates django-views


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    正确的方法名称是is_valid

    【讨论】:

      猜你喜欢
      • 2017-10-27
      • 2022-11-03
      • 1970-01-01
      • 2018-10-28
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多