0

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

视图.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))

表格.py

from django import forms
class LoginForm(forms.Form):
    username = forms.EmailField()
    password = forms.CharField(max_length=50)

索引.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>
4

2 回答 2

7

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

Django 文档

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')
于 2013-07-07T15:51:34.097 回答
2

正确的方法名称是is_valid.

于 2013-07-07T15:50:36.527 回答