Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4625144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:13:49+00:00 2026-05-22T03:13:49+00:00

The 2 types of validation error a form field in django can have are

  • 0

The 2 types of validation error a form field in django can have are ‘required’ and ‘invalid’. Is there any way to find out which of these two errors has happened, from the template? Something like

{% if form.username.errors %}
{% for error in form.username.errors %}
{% if error.required %}
Please enter the username
{% else %}
{{ error }}
{% endif %}

I just want to override the error message for the ‘required’ error, i.e., I want to display my own error message if that error happens. I am using django.contrib.auth.views which uses django.contrib.auth.forms.AuthenticationForm which I don’t want to try customizing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:13:49+00:00Added an answer on May 22, 2026 at 3:13 am

    You really should just override the Authentication form. The view accepts an argument that allows for you to override form easily.

    I think something like this should work:

    All you need to do is override the clean_username method like so:
    edit:

    overriding the clean_username method fails to change the validation error message because of the following from the form and field validation docs:

    • The clean() method on a Field
      subclass. This is responsible for
      running to_python, validate and
      run_validators in the correct order
      and propagating their errors. If, at
      any time, any of the methods raise
      ValidationError, the validation stops
      and that error is raised. This method
      returns the clean data, which is then
      inserted into the cleaned_data
      dictionary of the form.

    • The clean_<fieldname>() method in a
      form subclass – where is
      replaced with the name of the form
      field attribute. This method does any
      cleaning that is specific to that
      particular attribute, unrelated to the
      type of field that it is. This method
      is not passed any parameters. You will
      need to look up the value of the field
      in self.cleaned_data and remember that
      it will be a Python object at this
      point, not the original string
      submitted in the form (it will be in
      cleaned_data because the general field
      clean() method, above, has already
      cleaned the data once).

    The Field subclass is validated first and returns the cleaned data that is used for the clean_<field_name>() method. If an error occurs there the validation of that field stops.

    This means that to override the message you need to either override the Field validation or make the field not require a value so no validation error is raised at that step and raise a required method in the clean_<fieldname>() method

    >>> from django.contrib.auth.forms import AuthenticationForm
    >>> class MyAuthForm(AuthenticationForm):
    ...     def __init__(self, *args, **kwargs):
    ...         super(MyAuthForm, self).__init__(*args,**kwargs)
    ...         self.fields['username'].error_messages['required']='Custom Required Msg'
    ... 
    >>> form = MyAuthForm(data={'password':'asdf'})
    >>> form.is_valid()
    False
    >>> form.errors
    {'username': [u'Custom Required Msg']}
    >>> 
    

    urls.py

    from someapp.forms import MyAuthForm
    
    urlpatterns = patterns('',
        ...
        (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'someapp/login.html', 'authentication_form':MyAuthForm, }),
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i display the validation error messages just below the form field rather
I am using jquery validation plugin to validate my form. I have one field
I am new to rails validation. I have two fields: field :feed_entitlements, :type =>
What types and arguments does the method, Any when using Expression.Call take? I have
I have a page which displays a form that a logged-in user can use
I have a form containing a date and datetime field: @Html.TextBoxFor(model => model.A, new
I have some JQuery form validation where im checking the length of the username
I have a simple html form that I've added validation to using the JQuery
I recently tried extending django's registration form with the following but i can only
I have some script for image upload and I'm using it with form validation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.