form.is_valid()总是在Django

时间:2015-09-11 13:45:41

标签: django django-models django-forms django-views

下面是我提交表单的代码。当我提交表单时,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,我们非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

您似乎错过了一个&#34;名称&#34;属性在HTML文件中的字段上,因此该值实际上永远不会发布到Django。如果只分别将name="email"name="password"添加到字段中,则应该传递值并开始正确验证。

然而,话虽如此,我同意Alasdair的上述评论。使用Django内置的身份验证系统会更加安全和建议。