class PickyAuthenticationForm(AuthenticationForm): def confirm_login_allowed(self, user): if not user.is_active: raise forms.ValidationError( _("This account is inactive."), code='inactive', )
...
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:--
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.
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