【发布时间】:2015-09-11 13:45:40
【问题描述】:
下面是我提交表单的代码。当我提交表单时,is_valid 总是返回 false 不确定我的代码是否出错。我刚刚开始学习 Django,任何帮助都非常感谢 TIA
HTML
{%extends 'base.html'%}
{% block content %}
<div class="container">
<form method="post" class="form-signin" action="/loginvalidation/">{% csrf_token %}
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
{% endblock %}
HTML 文件
view.py
def loginvalidation(request):
print request
form = LoginssForm(request.POST or None)
print form.is_valid()
if form.is_valid():
save_it=form.save(commit=False)
print save_it.email
save_it.save()
user = authenticate(username='john', password='secret')
if user is not None:
# the password verified for the user
if user.is_active:
print("User is valid, active and authenticated")
else:
print("The password is valid, but the account has been disabled!")
else:
# the authentication system was unable to verify the username and password
print("The username and password were incorrect.")
return render(request,"about-us.html",locals(), context_instance=RequestContext(request))]
查看。 py 我的代码
Model.py
class LogIn(models.Model):
email=models.EmailField(null=False,blank=False)
password=models.CharField(max_length=1201,null=True,blank=True)
timestamp=models.DateTimeField(auto_now_add=True,auto_now=False)
updated=models.DateTimeField(auto_now_add=False,auto_now=True)
应用模型 应用模型应用模型
form.py
class LogInForm(forms.ModelForm):
class Meta:
model=LogIn
fields = '__all__'
以上是我提交表单的代码。当我提交表单时,form.is_valid 总是返回 False。我刚开始学习 Django,非常感谢任何帮助。
【问题讨论】:
-
如果你打印
form.errors,它会告诉你表格中的错误。但是,我强烈建议您不要尝试在 Django 中创建自己的身份验证系统。使用包含的authentication that comes with Django 更加安全。 -
Printing form.errors 得到了这个,但我正在从表单发送密码值
- password
- 此字段是必需的.
- 电子邮件
- 此字段为必填项。
- password
标签: django django-models django-forms django-views