User authentication with either username or email.
Hi,
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"
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)
Can anyone help me out in solving the issue?
Thanks. -- 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 http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home