【问题标题】:can't check username and email and password using UserCreationForm - Django无法使用 UserCreationForm 检查用户名、电子邮件和密码 - Django
【发布时间】:2021-07-04 15:31:40
【问题描述】:

当我将样式添加到UserCreationForm in forms.py 并将其传递给注册 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>

forms.py:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

# 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',
            ]

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

【问题讨论】:

  • 您是否考虑过使用django-crispy-forms 包进行造型?如果您使用django-crispy-forms,您可能不必在forms.py或模板中手动添加这么多代码,并且用于身份验证的内置django逻辑可能会更好。
  • 您是否收到错误消息?还是只是页面似乎只是重新加载?
  • 不,我不使用 django-crispy-forms
  • 不,我不明白..我可以填写我的注册表单并将我重定向到登录页面,但问题是我填写表格的数据不去数据库,我有表单的问题我无法检查电子邮件、密码和用户名是否存储在我的数据库中或没有
  • @KokHyvv 请在问题中添加您的观点。

标签: django django-rest-framework django-forms django-templates


【解决方案1】:

我已经编辑了我的代码并且工作正常

# 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 注册.html:

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

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 1970-01-01
    • 2013-01-18
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多