无法使用UserCreationForm检查用户名以及电子邮件和密码-Django [英] can't check username and email and password using UserCreationForm - Django

查看:81
本文介绍了无法使用UserCreationForm检查用户名以及电子邮件和密码-Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Forms.py 中将样式添加到 UserCreationForm中并将其传递到注册html页面并对其进行测试时,它无法正常工作,我的意思是不检查用户名和电子邮件(如果存在)还是不可以,密码不要检查密码是否正确

when i add style to UserCreationForm in forms.py and pass it to signup html page and test it , it don't work fine i mean it don't check username and email if it exist or not and password don't check if it good or no

singup.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',
            ]

我该如何修复检查用户名以及电子邮件和密码

how i can fix check username and email and password

推荐答案

我已经对此进行了编辑并可以正常工作

i have edit my code to this and work fine

# 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:

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>

这篇关于无法使用UserCreationForm检查用户名以及电子邮件和密码-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆