0

So I have a Django form and what I want is that it shows a message under (like in red) each field if they are not filled. I have tried adding the "error_messages" attribute to my widgets in each field but it won't work.

my forms.py:

class WordForm(forms.Form):
Word1 = forms.CharField(error_messages={'required': 'Please enter a word'}, required = True, label= 'Word1', 
    widget= forms.TextInput(attrs={
        'class': "form-control is-invalid",
        'placeholder': 'Enter First Word'
    }))
Word2 = forms.CharField(error_messages={'required': 'Please enter a word'}, required = True, label= 'Word2', 
    widget= forms.TextInput(attrs={
        'class': 'form-control is-invalid needs-validation was-validated',
        'placeholder': 'Enter Second Word'
    }))
Word3 = forms.CharField(error_messages={'required': 'Please enter a word'}, required = True, label= 'Word3', 
    widget= forms.TextInput(attrs={
        'class': 'form-control is-invalid',
        'placeholder': 'Enter Third Word'
    }))

Result = forms.CharField(label= 'Result', required=False, 
        widget= forms.TextInput(attrs={'class': 'form-control', 'readonly': 'True'}))

my form.html:

<form method="post" class="needs-validation was-validated" novalidate>
            {% csrf_token %}
            <h2>Inputs</h2>
            {{ form.as_p }}
</form>

As you can see, I have added the error messages field and also the is-invalid class in the widgets but It won't do anything. I don't really know if I have to create a function, cause I have seen those feedback messages but not with the form rendering. Help is much appreciated.

This is what my interface looks like: enter image description here

eneko valero
  • 457
  • 3
  • 14

1 Answers1

0

You need to create a template page for rendering form error messages. Your form_error.html should look like this:

<form method="post" class="needs-validation was-validated" novalidate>
  {% csrf_token %}
  <h2>Inputs</h2>
  {% for field in form %}
      {{ field.label_tag }}
      {{ field }}
      {% if field.errors %}
          {% for error in field.errors %} 
          <div class="your-css-alert-class">
              <strong>{{ error|escape }}</strong>
          </div>
          {% endfor %}
      {% endif %}
  {% endfor %}
</form>

Then in your view.py,

def submit(request):
    if request.method == "POST":
        ...
        if  form.is_valid():
            ...
        else:
            return render(request, 'form_error.html', {'form': form})

Relevant information in the documentation for rendering error messages

Didn't know this has been answered before

phyominh
  • 266
  • 1
  • 7
  • Yes you are kind of right, but does not really solve what I wanted to achieve. What I wanted is everytime I press the button, a red bootstrap text appears under each field. This help text appears everytime. @phyominh – eneko valero Feb 17 '21 at 13:58
  • Then you need to render your fields manually in an error template. I will update my answer on that. – phyominh Feb 17 '21 at 14:31
  • Okay thank you, take your time in answering! @phyominh – eneko valero Feb 17 '21 at 14:36
  • 1
    I've added the codes. Let me know if they solve your problem. – phyominh Feb 17 '21 at 14:56