-1

How can I pass custom form validation errors in template?

forms.py

    def clean_username(self):
    inputusername = self.cleaned_data['username']
    if len(inputusername) < 6:
    raise forms.ValidationError('Sorry, your username must be between 6 and 30 characters long.')
    else:
    return inputusername

views.py

def signup(request):
signup_form = CreateUserForm()
if request.method == 'POST':
signup_form = CreateUserForm(request.POST)
if signup_form.is_valid():
signup_form.save()

context = {'signup_form':signup_form}
return render(request, 'formvalidationapp/signupform.html', context)

temlate

<form method="post" action="{% url 'signup' %}">
{% csrf_token %}
<div class="row">
<div class="col-sm-6">
<h1>Sign Up</h1>
</div>
</div>
<div>
#i want to pass error here
</div>
1

1 Answer 1

1

You can try like this

{{signup_form.username.errors}}
Sign up to request clarification or add additional context in comments.

1 Comment

bro in this case how can i do? def clean_password(self): inputpassword1 = self.cleaned_data['password1'] inputpassword2 = self.cleaned_data['password2'] if inputpassword1 != inputpassword2: raise forms.ValidationError('Password didn\'t match') else: return inputpassword1,inputpassword2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.