ValidationError syntax different than in Django 1.9 docs

131 views
Skip to first unread message

Andrzej Olchawa

unread,
Feb 14, 2016, 10:31:51 AM2/14/16
to Django users
Hi guys,

first of all, I'm new to Django and this group.

I've just created my first Django login form by extending AuthenticationForm and overriding confirm_login_allowed method to do some extra checks and raise proper exceptions. I've done that based on an example from the Django 1.9 documentation (https://docs.djangoproject.com/en/1.9/topics/auth/default/#module-django.contrib.auth.forms):

class PickyAuthenticationForm(AuthenticationForm):
    def confirm_login_allowed(self, user):
        if not user.is_active:
            raise forms.ValidationError(
                _("This account is inactive."),
                code='inactive',
            )
...


Of course I imported the forms (it would be nice if I didn't have to figure out which forms should I import ...): 
from django import forms

This, however, issues a problem: "... confirm_login_allowed _("This account is inactive."), NameError: global name '_' is not defined".

So, despite what the doc says, I tweaked the code a bit to this, and it works:

class PickyAuthenticationForm(AuthenticationForm):
    def confirm_login_allowed(self, user):
        if not user.is_active:
            raise forms.ValidationError("This account is inactive.", code='inactive',)
...

I still don't understand why the previous one code didn't work. Is it a mistake in the documentation? I doubt it, and what I think instead is that I imported wrong forms. Instead of:

from django import forms

I should do:

from django.<something different here> import forms

and maybe then the syntax ValidationError would be as in the docs. 

Sergiy Khohlov

unread,
Feb 14, 2016, 1:38:52 PM2/14/16
to django-users

Many thanks,

Serge


+380 636150445
skype: skhohlov

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e02dea8c-f9c7-4850-a825-c14d775b1e8d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yudong zheng

unread,
Feb 14, 2016, 6:38:45 PM2/14/16
to Django users
Did u check indentation , python indentation might be tricky. u should look for PEP8 for details .

James Schneider

unread,
Feb 14, 2016, 9:37:33 PM2/14/16
to django...@googlegroups.com


On Feb 14, 2016 7:31 AM, "Andrzej Olchawa" <andrzej...@gmail.com> wrote:
>
> Hi guys,
>
> first of all, I'm new to Django and this group.
>
> I've just created my first Django login form by extending AuthenticationForm and overriding confirm_login_allowed method to do some extra checks and raise proper exceptions. I've done that based on an example from the Django 1.9 documentation (https://docs.djangoproject.com/en/1.9/topics/auth/default/#module-django.contrib.auth.forms):
>
> class PickyAuthenticationForm(AuthenticationForm):
>     def confirm_login_allowed(self, user):
>         if not user.is_active:
>             raise forms.ValidationError(
>                 _("This account is inactive."),
>                 code='inactive',
>             )
>
> ...
>
>
>
> Of course I imported the forms (it would be nice if I didn't have to figure out which forms should I import ...): 
> from django import forms
>
> This, however, issues a problem: "... confirm_login_allowed _("This account is inactive."), NameError: global name '_' is not defined".
>

Sergiy is correct about a translation issue, but this could stand a bit of clarification. The tutorial mentions this, but kind of glazes over it. Your problem actually is due to the leading underscore for the ValidationError text, which Python is complaining that you haven't defined. Read this section: https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#standard-translation

Add 'from django.utils.translation import ugettext as _' to the top of your file, and that should straighten you out. The convention in Django is to use a single underscore as a shortcut method call for ugettext().

-James

Andrzej Olchawa

unread,
Feb 21, 2016, 10:03:29 AM2/21/16
to Django users

Many thanks to all of you for your answers. I had no idea Django has such a translation feature. I'm starting to like this technology more and more every day :-)

Cheers
Andrzej
Reply all
Reply to author
Forward
0 new messages