[Solved]-Form validation in django

18👍

You should ALWAYS validate your form on the server side, client side validation is but a convenience for the user only.

That being said, Django forms has a variable form.errors which shows if certain form fields were incorrect.

{{ form.name_of_field.errors }} can give you each individual error of each field that’s incorrectly filled out. See more here:

http://docs.djangoproject.com/en/dev/topics/forms/

6👍

There’s a pluggable Django app (django-ajax-forms) that helps validate forms on the client side through JavaScript. But as AlbertoPL says, use client side validation only as a usability measure (e.g. telling a user that his desired username is already taken without reloading the registration page). There are all kind of ways to sidestep client side validation, in most cases as simple as deactivating JavaScript.

Generally speaking: presume all data coming from the outside as faulty until validated.

4👍

Just came accross django-floppyforms, which seems to do clientside validation by default. They use HTML5 which supports clientside validation by default. Not sure if they also use javascript though if the browser doesn’t support HTML5. Haven’t tried it myself yet.

Link to django-floppyforms: Documentation and Github

2👍

If you are using bootstrap then you can simply add required attribute in forms field. For example if there is programe field then you can validate it like:

In forms.py:

programme = forms.ChoiceField(course_choices,required=True, widget=forms.Select(attrs={'required':'required'}))

Note: It requires to link to bootstrap files in your .html page of that form.

1👍

You will need to do this is JS. This app integrates forms with parsley.js to tag the forms with correct data-* attributes automatically.

👤shabda

Leave a comment