Sunday, February 14, 2016

Re: ValidationError syntax different than in Django 1.9 docs


Many thanks,

Serge


+380 636150445
skype: skhohlov

On Sun, Feb 14, 2016 at 5:23 PM, Andrzej Olchawa <andrzejolchawa@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".

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. 

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CADTRxJMhbe-iA7s9YC_rtBcoG_VTfBBTsXpnCoh1K9naW2ikww%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment