Re: User authentication with either username or email.
This helped alot and solved 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+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.
Re: User authentication with either username or email.
On Fri, Jun 14, 2013 at 3:29 PM, shashank sandelawrote: > As you can see in the views.py I did import the class > EmailOrUsernameModelBackend. OK. But you mustn't import that class, you must allow django to import it itself, and then django will use it when authenticating users. You do this by specifying the class in settings.AUTHENTICATION_BACKENDS, and then calling the function authenticate() in your view, which you should import from django.contrib.auth. You are trying to call the method authenticate on your auth backend directly. Do not do this, it is incorrect. It is because of this you are getting the error "'User' object has no attribute 'backend'". Please notice the difference between the **function** authenticate() and the **method** authenticate() on your auth backend. Your view should look like the view linked below: https://docs.djangoproject.com/en/1.5/topics/auth/default/#how-to-log-a-user-in 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.
Re: User authentication with either username or email.
Sorry. The error was: AttributeError at /authentication/ 'User' object has no attribute 'backend' -- 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.
Re: User authentication with either username or email.
As you can see in the views.py I did import the class * EmailOrUsernameModelBackend.* * * Now when I used " *user = EmailOrUsernameModelBackend().authenticate(username=username, password=password) *" It gave an error saying: AttributeError at /tangle/auth/ 'User' object has no attribute 'backend' -- 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.
Re: User authentication with either username or email.
El 14/06/2013 07:26 a.m., shashank sandela escribió: unbound method authenticate() must be called with EmailOrUsernameModelBackend instance as first argument (got nothing instead) Hi. Remember that you must import a function before you call it. This is a reference to authentication in Django https://docs.djangoproject.com/en/1.5/topics/auth/default/#how-to-log-a-user-in Regards. -- 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.
Re: User authentication with either username or email.
On Fri, Jun 14, 2013 at 12:56 PM, shashank sandelawrote: > 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.
User authentication with either username or email.
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" 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+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.