On Fri, Jun 14, 2013 at 12:56 PM, shashank sandela
<shashanksandel...@gmail.com> wrote:
> Hi,
>
> I created a backends.py in my project folder.
>
> backends.py ::
>
> from django.conf import settings
> from django.contrib.auth.models import User
>
> class EmailOrUsernameModelBackend(object):
>     def authenticate(self, username=None, password=None):
>         if '@' in username:
>             kwargs = {'email': username}
>         else:
>             kwargs = {'username': username}
>         try:
>             user = User.objects.get(**kwargs)
>             if user.check_password(password):
>                 return user
>         except User.DoesNotExist:
>             return None
>
>     def get_user(self, user_id):
>         try:
>             return User.objects.get(pk=user_id)
>         except User.DoesNotExist:
>             return None
>
> And in the views.py,
>
> from django.contrib import auth
> from django.contrib.auth.models import User
> from tangle.backends import EmailOrUsernameModelBackend
>
> def authentication(request):
>     username = request.POST.get('username', '')
>     password = request.POST.get('password', '')
>     user = authenticate(username=username, password=password)
>     if user is not None:
>         auth.login(request, user)
>         return HttpResponseRedirect('/loggedin/')
>     else:
>         return HttpResponseRedirect('/invalid_login')
>
> Settings.py
>
> AUTHENTICATION_BACKENDS = (
>     'backends.EmailOrUsernameModelBackend',
>     'django.contrib.auth.backends.ModelBackend'
> )
>
> Now when I login using either username or email it shows an error "global
> name 'authenticate' is not defined"

Yes. You must import this function before you can call it:

https://docs.djangoproject.com/en/1.5/topics/auth/default/#how-to-log-a-user-in

>
> So, I used
> user = EmailOrUsernameModelBackend.authenticate(username=username,
> password=password)
>
> It shows
>
> TypeError at /authentication/
>
> unbound method authenticate() must be called with
> EmailOrUsernameModelBackend instance as first argument (got nothing instead)
>

The EmailOrUsernameModelBackend.authenticate() method is an instance
method. You must call it with an instance of
EmailOrUsernameModelBackend, not as a class method. In fact, you do
not call it at all, you list the auth backends in settings.py, and
allow django to instantiate an instance of the class and call
authenticate on it.

Cheers

Tom

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to