views:

19

answers:

1

I'm using the standard authentication form. It's clean method looks like this :

def clean(self):
    username = self.cleaned_data.get('username')
    password = self.cleaned_data.get('password')

    if username and password:
        self.user_cache = authenticate(username=username, password=password)
        if self.user_cache is None:
            raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive."))
        elif not self.user_cache.is_active:
            raise forms.ValidationError(_("This account is inactive."))

    # TODO: determine whether this should move to its own method.
    if self.request:
        if not self.request.session.test_cookie_worked():
            raise forms.ValidationError(_("Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in."))

    return self.cleaned_data

My login form :

<form method="post" action="{% url django.contrib.auth.views.login %}">
    <table>
        {% if form.errors %}
            <tr class="form-errors">
                <td>
                    <ol>
                        {% for error in form.errors %}
                            <li>{{ error }}</li>
                        {% endfor %}
                    </ol>
                </td>
            </tr>
        {% endif %}

But instead of the validation messages I get :

if username and pass is incorrect or account is inactive : "1. __all__"
if password is not given : "1. password"
if username is not given : "1. username"
if both are not given : "1. username 2. password"
no message for incorrect username

EDIT: If I change

                <td>
                    <ol>
                        {% for error in form.errors %}
                            <li>{{ error }}</li>
                        {% endfor %}
                    </ol>
                </td>

to :

{{ form.errors }}

I receive i.e __all__ and beneath it "Account inactive". How to get just the message ? Any ideas ?

+1  A: 

Try this. form.errors is a dictionary, so like every dictionary you can read key and value. Key is field or __all__' and value is the error message you were looking for.

{% if form.errors %}
<tr>
    {% for k, v in form.errors.items %}
    <td>{{k}}</td>
    <td>{{v}}</td>
    {% endfor %}
</tr>
{% endif %}

EDIT

If you wan't to select specific field error types :

{% if form.errors %}
    {% for k, v in form.errors.items %} 
        <tr>
            {% ifequal k 'password' %}
                <td>Password</td>
                {% else %}
                    {% ifequal k 'username' %}
                        <td>Username</td>
                        {% else %}
                            <td>Other</td>
            {% endifequal %}{% endifequal %}                
            <td>{{v}}</td>
        </tr>
    {% endfor %}
{% endif %}

or select '__all__' in ifequal .

crivateos
now gotta select to only take `__all__` errors :/
muntu
edited my answer
crivateos