python - Return error message to custom form in Django -


the fields in custom registration form looks this:

<div class="form-group">           {{ form.email.errors }}         label for="{{form.email.id_for_label}}">         <b>email</b>         </label>           {{ form.email }} </div> 

my views.py looks this:

def registration(request):     registered = false     if request.method == 'post':         form = regform(data=request.post)         if form.is_valid():             email = form.cleaned_data['email']             phone = form.cleaned_data['phone']             password = form.cleaned_data['password']             password2 = form.cleaned_data['password2']             user = user()             user.email = email             user.username = email             user.is_active = false             user.set_password(password)             user.save()             return render(request, 'appname/index.html', {'registered': true})     else:         form = regform()         render(request, 'appname/index.html', {'form': form}) 

forms.py:

class regform(forms.form):     email = forms.emailfield(error_messages={'required': 'please enter email id'})     password=forms.charfield(widget=forms.passwordinput())     password2=forms.charfield(widget=forms.passwordinput())     phone= forms.charfield(label="mobile number",max_length=10)  #clean email field     def clean_email(self):         email = self.cleaned_data["email"]         try:             user._default_manager.get(email=email)         except user.doesnotexist:             return email         raise forms.validationerror('duplicate email') 

let's if email exisiting in db, how return error message custom form , display in place of {{form.email.errors}}

edit
have used validationerror in forms.py , returning raise forms.validationerror('duplicate email') if email exists. how show error in custom form?

you write custom cleaner email field in regform. like:

class regform(forms.form):     # before.     ...      #clean email field     def clean_email(self):         email = self.cleaned_data["email"]         if user.objects.filter(email=email).exists():             raise forms.validationerror('duplicate email')         return email 

this documented here.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -