Override Django Form.is_valid()

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.


You can then simply use form.is_valid() in your view:


6 thoughts on “Override Django Form.is_valid()”

  1. How do you display the errors in the template?
    using ” {{ form.errors }} ” causes it to display errors like this “invalid_passwordPassword is invalid”

    1. You can display the errors for a specific field like this:

      {{ form.fieldname.errors }}

      You can optionally use striptags to remove any Django formatting:

      {{ form.fieldname.errors|striptags }}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s