1
我創建了一個自定義的登記表:ValidationError消息未出現在我自己的形式
class MyRegistrationForm(UserCreationForm):
mobile = models.CharField(max_length=16)
address = models.CharField(max_length=100)
class Meta():
model = CustomUser
fields = ['username', 'password1', 'password2', 'first_name', 'last_name', 'email', 'mobile', 'address']
def clean_mobile(self):
mobile = self.cleaned_data['mobile']
if CustomUser.objects.filter(mobile=mobile).exists():
raise ValidationError('Mobile already exists')
return mobile
def save(self, commit=True):
user = super(MyRegistrationForm, self).save(commit=False)
user.mobile = self.cleaned_data['mobile']
user.address = self.cleaned_data['address']
if commit:
user.save()
return user
而且在我的模板,我不寫只是`{{形式}}」,而不是我用我的引導模板註冊。它看起來像:
<form class="form-horizontal" action="{% url 'register' %}" method="post">
{% csrf_token %}
<div class="control-group">
<label class="control-label" for="username1" >Ім'я користувача <sup>*</sup></label>
<div class="controls">
<input type="text" id="username1" placeholder="Нік" name="username">
</div>
</div>
<div class="control-group">
<label class="control-label" for="password_1" >Пароль <sup>*</sup></label>
<div class="controls">
<input type="password" id="password_1" placeholder="Введіть пароль" name="password1">
</div>
</div>
<div class="control-group">
<label class="control-label" for="password_2" >Повторіть пароль <sup>*</sup></label>
<div class="controls">
<input type="password" id="password_2" placeholder="Повторіть пароль" name="password2">
</div>
</div>
但當clean_mobile
方法被調用,提高ValidationError
,沒有錯誤不顯示。如何解決這個問題?或者,也許我可以在模板{{form}}
中使用,但我需要使用引導CSS?