For this project I won’t be using Django’s built-in user authentication application. It’s a bit restrictive for my taste (but getting better in Django 1.5 from what I can tell).
One common use case is to validate the user’s username and password when logging in. Rather than place this logic within the view, it’s cleaner to override the default is_valid method with some additional logic that checks the password.
Here’s an example form with a username and password field. The username field allows either a username or email address, and I’m using Django’s built-in password hasher.
forms/signin.py
You can then simply use form.is_valid() in your view:
views/signin.py
How do you display the errors in the template?
using ” {{ form.errors }} ” causes it to display errors like this “invalid_passwordPassword is invalid”
You can display the errors for a specific field like this:
You can optionally use striptags to remove any Django formatting:
Thanks. works perfect!!!
Thanks 🙂 Worked like a charm
Your post helped me a bunch, thanks! And the new way to properly inject a custom error message on a field is using self.add_error(field_name, error_message)
http://stackoverflow.com/questions/5083493/is-valid-vs-clean-django-forms/40416585#40416585
Never use self._errors but instead self.add_error, like self.add_error(’email’, ‘Email is already in use’).