Hem*_*S R 0 python django django-templates django-forms django-views
如果我打印request.POST.username和request.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)
您没有正确使用该方法.正确的方法是is_valid
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)
| 归档时间: |
|
| 查看次数: |
2132 次 |
| 最近记录: |