django - ValidationError message doesn't appear in my own form -
i created custom registration form:
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 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
and in template, don't write `{{form}}', instead use bootstrap template registration. looks like:
<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>
but when clean_mobile
method called , raise validationerror
, no errors aren't showed. how solve issue? or maybe can use in template {{form}}
need use bootstrap css?
you need include {{ form.mobile.errors }}
display errors mobile
field.
see working form templates docs more information.
you might want investigate django crispy forms, makes easy render bootstrap forms.
Comments
Post a Comment