Django forms: Error message display all the time, not only after errors
<p>I'm building my own site with Django's Framework.
I made a form for creating a new account. It works well but I have a problem with my errors messages.</p>

<p>All my errors are already display when I first arrive on the page. After submit, if a field is wrong, the page newly updated display only the wanted messages.</p>

<p><a href="http://i.stack.imgur.com/tfOkK.png" rel="nofollow">Here is a view of my page at my first arrival</a></p>

<p>So, my question is: How can I do for display the message only if a field is wrong ? I already succeed to personalize each messages.</p>

<p>I paste you my different django files:</p>

<p><strong>views.py</strong></p>

<pre><code>@csrf_exempt
def create_new_user(request):
form = UserCreateForm(request.POST)
if request.method=="POST":
    if form.is_valid():
        user = form.save()
        messages.info(request, "Merci pour votre enregistrement, vous etes maintenant connecte")
        new_user = authenticate(username=form.cleaned_data['username'],
        password=form.cleaned_data['password1']
        )
        login(request, new_user)
        return HttpResponseRedirect('/')
    return render_to_response('lejeudesbars/register.html', RequestContext(request, {'form': form}))
else:
    return render(request, 'lejeudesbars/register.html', {'form': form})
</code></pre>

<p><strong>forms.py</strong></p>

<pre><code>class UserCreateForm(UserCreationForm):
    captcha = ReCaptchaField(error_messages={'required': 'Captcha: Validation obligatoire'})
    email = forms.EmailField(required=True)
    username = forms.CharField(error_messages={'required': 'Pseudo: Champ obligatoire'})
    email = forms.EmailField(error_messages={'required': 'Email: Champ obligatoire'})
    password1 = forms.CharField(widget=forms.PasswordInput(), error_messages={'required': 'Mot de passe: Champ obligatoire'})
    password2 = forms.CharField(widget=forms.PasswordInput(), error_messages={'required': 'Mot de passe: Confirmation obligatoire'})


    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")

    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user`
</code></pre>

<p><strong>index.html</strong></p>

<pre><code>    &lt;form method="post" action="/accounts/register/"&gt;
        {% csrf_token %}
        &lt;h1&gt;S'enregistrer&lt;/h1&gt;

        {{ form.username }}
        {% if form.errors %}
        &lt;p class="error-msg-register"&gt;{{ form.username.errors }}&lt;/p&gt;
        {% endif %}

        {{ form.email }}
        {% if form.errors %}
        &lt;p class="error-msg-register"&gt;{{ form.email.errors }}&lt;/p&gt;
        {% endif %}

        {{ form.password1 }}
        {% if form.errors %}
        &lt;p class="error-msg-register"&gt;{{ form.password1.errors }}&lt;/p&gt;
        {% endif %}

        {{ form.password2 }}
        {% if form.errors %}
        &lt;p class="error-msg-register"&gt;{{ form.password2.errors }}&lt;/p&gt;
        {% endif %}

        {{ form.captcha }}
        {% if form.errors %}
        &lt;p class="error-msg-register"&gt;{{ form.captcha.errors }}&lt;/p&gt;
        {% endif %}

        &lt;input style="padding: 10px" type="submit" value="CrÃ©er mon compte" /&gt;
        &lt;input type="hidden" name="next" value="{{ next }}" /&gt;
  &lt;/form&gt;`
</code></pre>

<p>Thank you in advance for helping me !</p>

<p>(sorry for my bad english)</p>

<hr>

<p>Thanks to Daniel Roseman, I've resolved my problem.
Here is my updated views.py:</p>

<p><strong>views.py</strong></p>

<pre><code>@csrf_exempt
def create_new_user(request):
    if request.method=="POST":
        form = UserCreateForm(request.POST)
        if form.is_valid():
            user = form.save()
            messages.info(request, "Merci pour votre enregistrement, vous etes maintenant connecte")
            new_user = authenticate(username=form.cleaned_data['username'],
            password=form.cleaned_data['password1']
        )
            login(request, new_user)
    else:
         form = UserCreateForm()
    return render(request, 'lejeudesbars/register.html', {'form': form})
</code></pre>

<p>In each field you have {% if form.errors %} - so when there is at least one error, you are going to display it everywhere.</p>

<p>change it to <code>{% if form.field_name.errors %}</code> and then html should render only the errors associated with that specific field</p>

python django forms