在django中对Registrationn表单进行身份验证

时间:2018-11-08 13:17:15

标签: django

我有此html表单,希望用于注册客户。

<form id='registration-form'>
                    {% csrf_token %}
                    <div class="form-group">
                      <input type="text" class="form-control input-upper" id="fullname" placeholder="John Doe" name="fullname" ><br>
                      <input type="text" class="form-control input-upper" id="username" placeholder="Username" name="username"><br>
                      <input type="email" class="form-control input-upper" id="email" placeholder="Email" name="email"><br>
                      <input type="text" class="form-control input-upper" id="organization" placeholder="Organization" name="organization"><br>
                      <input type="password" class="form-control input-upper" id="password" placeholder="Password" name="password"><br>
                      <input type="password" class="form-control input-upper" id="password" placeholder="Confirm Password" name="password"><br>
                      <small>By registering you agree to our <a href="{% url 'tos' %}">terms and conditions</a></small>
                      <button type="button" class="btn btn-primary btn-block btn-signup-form">SIGN UP</button>
                      <button type="button" class="btn btn-primary btn-block btn-sign-linkedin" href="{% url 'social:begin' 'linkedin-oauth2' %}?next={{ next }}">Sign up with LinkedIn</button>
                      <p class="text-already">Already have an account? <a href="">LOGIN</a></p>
                    </div>
                </form>

我如何验证填写的数据(即电子邮件和密码),并希望客户在注册后能够登录

1 个答案:

答案 0 :(得分:1)

我不确定验证数据的含义,但是如果我正确理解了,则应该使用Django内置的功能来创建用户。 Django随附了Auth模块,可以减少您的工作量,并可以处理大多数痛苦的部分。看看这篇帖子https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html

如果您想要简单的验证,则可以使用Django Form的干净方法。用您提到的相同字段编写一个表单类。 例如。

Class  SignupForm2(forms.ModelForm ):
  class Meta:
       model = User 

  def clean(self):
      self.cleaned_data = super(SignupForm2, self).clean()

        if 'captcha' in self._errors and self._errors['captcha'] != ["This field is required."]:
        self._errors['captcha'] = ["Please enter a correct value"]
    return self.cleaned_data