[Answer]-Django how to use template tags

1👍

In your form, it may help to have a clean method:

def clean(self):
    first_name = self.cleaned_data.get('first_name')

    if password is None:
        raise forms.ValidationError('This is a custom error message.')

    return self.cleaned_data

Then, in your template, you can have code like:

    {{ form.first_name }}
    {% if form.first_name.errors %}
         {{form.first_name.errors.as_text}}
    {% endif %}

Otherwise, django’s default form validation will treat all your inputs as required, and provide that generic message.

Another option, from the docs, is to define error messages at the form field level: https://docs.djangoproject.com/en/dev/ref/forms/fields/

name = forms.CharField(error_messages={'required': 'Please enter your name'})

This will add the custom error message based on the error type, so in your case, you could use something like:

first_name = models.CharField(max_length=50, verbose_name='first Name', validators=[alpha_field], error_messages={'required': 'Please enter a first name'})
👤james

Leave a comment