Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
can';t使用UserCreationForm-Django检查用户名、电子邮件和密码_Django_Django Rest Framework_Django Forms_Django Templates - Fatal编程技术网

can';t使用UserCreationForm-Django检查用户名、电子邮件和密码

can';t使用UserCreationForm-Django检查用户名、电子邮件和密码,django,django-rest-framework,django-forms,django-templates,Django,Django Rest Framework,Django Forms,Django Templates,当我在forms.py中将样式添加到UserCreationForm中,并将其传递到注册html页面并进行测试时,它不会正常工作,我的意思是它不会检查用户名和电子邮件是否存在,密码也不会检查它是否正确 singup.html: <form method="post"> {% csrf_token %} {% for field in form %} <fieldset class="control-group">

当我在forms.py中将样式添加到
UserCreationForm中,并将其传递到注册html页面并进行测试时,它不会正常工作,我的意思是它不会检查用户名和电子邮件是否存在,密码也不会检查它是否正确

singup.html:

<form method="post">
    {% csrf_token %}
    {% for field in form %}
  <fieldset class="control-group">
     <label class="control-label" for="id_{{ field.name }}">{{ field.label }}</label>
      <div class="controls">
          {{ field }}
          <p class="help-text">{{ field.help_text }} </p>
      </div>
  </fieldset>
  {% endfor %}
  <input type="submit" class="fadeIn fourth" style="background-color:#7952b3" value=" sign up ">
</form>

如何修复检查用户名、电子邮件和密码

我已将我的代码编辑为该密码,工作正常

# Sign Up Form
class SignUpForm(UserCreationForm):
    first_name = forms.CharField(required=True,label=' First Name : ',widget=forms.TextInput(attrs={'class': 'form-control'}) )
    last_name = forms.CharField(required=True,label=' Last Name : ',widget=forms.TextInput(attrs={'class': 'form-control'}) )
    email = forms.EmailField(required=True,label=' Email ',widget=forms.EmailInput(attrs={'class': 'form-control center container','style': 'width:85%;text-align: center;background-color:#f6f6f6'}) )

    # constructor of the UserForm, not Meta
    def __init__(self, *args, **kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].widget.attrs.update({'class':'form-control','placeholder':' add username ','style': 'font-size:19px;text-align: center;'})
        self.fields['password1'].widget.attrs.update({'class':'form-control','placeholder':' enter pass ','style': 'font-size:19px;text-align: center;'})
        self.fields['password2'].widget.attrs.update({'class':'form-control','placeholder':' re-type pass ','style': 'font-size:19px;text-align: center;'})

    class Meta:
        model = User
        fields = [
            'username',
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2',
            ]
    def clean_email(self):
        email = self.cleaned_data.get('email')
        if email and User.objects.filter(email=email).count():          raise forms.ValidationError('Email is already in use, please check the email or use another email')
        return email
html signup.html:

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Register</button>
    <br><br>
    <a href="{% url 'home' %}">Home</a>
</form>

{%csrf_令牌%}
{{form.as_p}}
登记



您是否考虑过使用
django crispy forms
包装进行造型?如果您使用
django crispy forms
,您可能不必在
forms.py
或模板中手动添加这么多代码,用于身份验证的内置django逻辑可能工作得更好。是否出现错误?或者仅仅是页面看起来好像要重新加载?不,我不使用django crispy Forms,我不明白。。我可以填写我的注册表格并将我重定向到登录页面,但问题是我填写的表格中的数据没有进入数据库,我的表格有问题,我无法检查电子邮件、密码和用户名,如果它们存储在我的数据库或no@KokHyvv请将您的观点添加到问题中。
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Register</button>
    <br><br>
    <a href="{% url 'home' %}">Home</a>
</form>