One quick thought is that your user query is case-sensitive (by default, Django queries use __exact: https://docs.djangoproject.com/en/dev/ref/models/querysets/#std:fieldlookup-exact ). So if the email was stored with mixed case, or if you're entering mixed case when logging in, it won't work. Try email__iexact=username (case-insensitive match) instead.
_Nik On 4/24/2012 10:52 AM, Nikolas Stevenson-Molnar wrote: > Could you provide a bit more detail about the problem? By 'not working' > do you mean you get an exception, or that you enter your email/password > and it doesn't log you in as expected? > > In the latter case, I would suggest a little debugging. First, make sure > your settings are correct and that your code is actually being executed. > For example, you could stick a print statement in your authenticate() > method and watch for it when logging in. If you don't see your statement > in the output, then your settings are likely wrong, if you do see it > then next check to make sure your query is returning a user, etc. until > you've narrowed down the problem. > > _Nik > > On 4/23/2012 2:20 AM, Mai wrote: >> here i guess i wrote Every thing to be able to log in by mail but it's >> not working is there something missing please? and >> AUTHENTICATION_BACKENDS = ( >> 'mayapp.backends.EmailAuthBackend', >> 'django.contrib.auth.backends.ModelBackend' >> ) >> >> >> backends.py >> >> >> from django.conf import settings >> from django.contrib.auth.backends import ModelBackend >> from django.core.validators import email_re >> from django.contrib.auth.models import User, check_password >> >> # Overwrite the default backend to check for e-mail address >> class EmailAuthBackend(object): >> """ >> Email Authentication Backend >> >> Allows a user to sign in using an email/password pair rather than >> a username/password pair. >> """ >> >> def authenticate(self, username=None, password=None): >> """ Authenticate a user based on email address as the user >> name. """ >> try: >> user = User.objects.get(email=username) >> if user.check_password(password): >> return user >> except User.DoesNotExist: >> return None >> >> def get_user(self, user_id): >> """ Get a User object from the user_id. """ >> try: >> return User.objects.get(pk=user_id) >> except User.DoesNotExist: >> return None >> >> >> views.py >> >> >> >> def login_user(request): >> state = "Please log in below..." >> username = password = '' >> if request.POST: >> if 'login' in request.POST: >> username = request.POST.get('username') >> password = request.POST.get('password') >> >> user = authenticate(username=username, >> password=password) >> if user is not None: >> if user.is_active: >> login(request, user) >> state = "You're successfully logged in!" >> >> return >> render_to_response('master.html',RequestContext(request)) >> else: >> state = "Your account is not active, >> please contact the site >> admin." >> else: >> state = "Your username and/or password were >> incorrect." >> elif 'signup' in request.POST: >> form= SignUpForm() >> context = {'form':form} >> return >> render_to_response('Sign_up_Employer.html',context,context_instance=RequestContext(request)) >> >> >> >> return render_to_response('login.html',{'state':state, 'username': >> username},RequestContext(request)) >> -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.