为什么form.is_valid()总是返回字段是必需的?

时间:2018-04-26 08:33:16

标签: django django-forms

我不知道为什么我的表单在提交后总是返回手机是必需的,我在一小时内遇到这个问题:(,我看过很多文章和问题已被问过但无济于事。

views.py

def register(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST or None)
        if form.is_valid():
            user = form.save(commit=False)
            telephone = number_prettify(form.cleaned_data.get('telephone'))
            user.phone = telephone
            raw_password = form.cleaned_data.get('password1')
            user.save()
            user = authenticate(phone=user.phone, password=raw_password)
            auth_login(request, user)
            return redirect('/')
    else:
        form = SignUpForm()
    return render(request, 'register.html', {'form': form})

这是我的 forms.py

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm

# from .models import Client

User = get_user_model()

class SignUpForm(UserCreationForm):
    telephone = forms.CharField(label='Номер телефона', help_text='Required. Inform a valid phone number.', 
                    widget=forms.TextInput(attrs={'placeholder': '___-__-__-__','class':"uk-input phone_us"}))

    password1 = forms.CharField(label='Пароль',widget=forms.PasswordInput())
    password2 = forms.CharField(label='Повторите пароль',widget=forms.PasswordInput())

    agree_1 = forms.BooleanField()
    agree_2 = forms.BooleanField()

    def clean(self):
        if not self.cleaned_data['agree_1']:
            raise forms.ValidateError('You have to agree to out privacy')

    class Meta:
        model = User
        fields = ('phone',)

这里也是 models.py

class Client(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(null=True, blank=True, unique=True)
    phone = models.CharField(max_length=18,unique=True)
    joined = models.DateTimeField(auto_now_add=True)
    password = models.CharField(default='', null=False, max_length=255)

    USERNAME_FIELD = 'phone'

    class Meta:
        db_table = 'client'
        unique_together = ('phone',)

我的 register.html 模板:

<div class="uk-margin">
    <label> 
        <div class="uk-margin-small-bottom">{{form.telephone.label}} <span>*</span></div>
        {{form.telephone}}
    </label>
    </div>
    <div class="uk-margin">
    <label> 
        <div class="uk-margin-small-bottom">{{form.password1.label}} <span>*</span></div>
        {{form.password1|add_class:"uk-input"}}
    </label>
</div>

1 个答案:

答案 0 :(得分:1)

您应该将字段从telephone更改为phone

class SignUpForm(UserCreationForm):
    phone = forms.CharField(label='Номер телефона', help_text='Required. Inform a valid phone number.', 
                    widget=forms.TextInput(attrs={'placeholder': '___-__-__-__','class':"uk-input phone_us"}))

    password1 = forms.CharField(label='Пароль',widget=forms.PasswordInput())
    password2 = forms.CharField(label='Повторите пароль',widget=forms.PasswordInput())

    agree_1 = forms.BooleanField()
    agree_2 = forms.BooleanField()

    def clean(self):
        if not self.cleaned_data['agree_1']:
            raise forms.ValidateError('You have to agree to out privacy')

    class Meta:
        model = User
        fields = ('phone',)