0

I have two forms in single template, and I want to display separate error msg independent from each other.

index.html

<form id="signin" action="{% url auth_login %}" method="post" accept-charset="utf-8">
{% csrf_token %}
 <fieldset id="signin_menu">
 <label for="username">Username</label>
 <input id="username" name="username" value="" title="username"type="text">
 </fieldset>
</form>

<form action="/accounts/register/" method="post" accept-charset="utf-8">
{% csrf_token %}
<fieldset id="register_set">
 <label for="username">Username</label>
 <input id="username" name="username" value="" title="username"type="text">
 </fieldset>
</form>

I tried this code:

{% if form.username.errors %}
    {% for error in form.username.errors %}
<span class="error_message">{{ error|escape }}  </span>
{% endfor %}
{% endif %}

Since I am using same username. It displays error msg on both form, if I place the code above.

1
  • 1
    Are the login views custom? If so rather then passing both forms to the template named form you could have form_a and form_b, meaning you can make the template code independent e.g. form_a.username.errors ? Commented Jul 14, 2011 at 9:21

1 Answer 1

1

Django has no way of knowing which form you're referring to in the DOM when each form has the same fields with the same name. You also have two fields with the same ID, which is invalid HTML. The value of the ID attribute must be unique. I would recommend prefixing your forms to keep the namespaces separate.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.