Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Very easy, you have multiple ways to display the error messages:</p> <p><strong>First one:</strong></p> <p>1- Override the clean method inside your form/modelform:</p> <pre><code>def clean(self): # To keep the main validation and error messages super(your_form_name, self).clean() # Now it's time to add your custom validation if len(self.cleaned_data['password']) &lt; 10: self._errors['password']='your password\'s length is too short' # you may also use the below line to custom your error message, but it doesn't work with me for some reason raise forms.ValidationError('Your password is too short') </code></pre> <p><strong>Second One</strong></p> <p>By using django built in validators.</p> <p>You can use the validators inside the models with the custom error message you want like this:</p> <pre><code>RE = re.compile('^[0-9]{10}$') field_name = models.CharField('Name', default='your_name',validators=[RegexValidator(regex=RE, message="Inapproperiate Name!")],) </code></pre> <p>where validators can utilize multiple built-in validation rules that you can validate against.</p> <p><a href="https://docs.djangoproject.com/en/dev/ref/validators/" rel="nofollow">please refer to this django documentation</a></p> <p>also you can use validators inside your forms definition, but the error message inside validators will not work as it did with models in case of errors, to custom error message use the below third method.</p> <p><strong>Third Method:</strong></p> <pre><code>skill = forms.CharField( label = 'SSkill', min_length = 2, max_length = 12, # message kwarg is only usable in models, yet, #it doesn't spawn error here. #validators=[RegexValidator(regex=NME, message="In-approperiate number!")], required = True, #Errors dictionary error_messages={'required': 'Please enter your skill','min_length':'very short entry','invalid':'Improper format'}, #Similar to default you pass in model initial = 'Extreme Coder' ) </code></pre> <p>Where, requied, min_length, max_length, along with others, are all fomr kw arguments, where the invalid will only be selected when validators a couple of lines above didn't match the criteria you selected inside validators.</p> <p><strong>Fourth Method</strong></p> <p>In this method, you will override your modelform's definition like this:</p> <pre><code>def __init__(self, *args, **kwargs): super(Your_Form, self).__init__(*args, **kwargs) self.fields['password'].error_messages['required'] = 'I require that you fill out this field' </code></pre> <p><strong>Fifth Method:</strong></p> <p>I have seen some nice way to custom errors in one of the stackoverflow posts like this:</p> <pre><code>class Meta: model = Customer exclude = ('user',) fields = ['birthday','name'] field_args = { "name" : { "error_messages" : { "required" : "Please let us know what to call you!" "max_length" : "Too short!!!" } } } </code></pre> <h2>reference to fifth method is: <a href="http://blog.brendel.com/2012/01/django-modelforms-setting-any-field.html" rel="nofollow">Brendel's Blog</a></h2> <p>May be there are other/better ways of doing things, but those are the most common, at least to me :-)</p> <p>I hope this post helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload