Django Custom Backend : Cannot Login when using custom backend for authentication with email
<p>My primary requirement is to have Email authentication. Though my custom authentication backend seems to work fine, <code>user.is_anoynymous()</code> function is still <code>True</code> even after succesfully authenticating with my custom backend. When using the default authentication model everything seems fine.</p>

<p>views.py:</p>

<pre><code> from testapp.models import *
 from django.http import HttpResponseRedirect,HttpResponse,Http404,QueryDict
 from django.shortcuts import    render_to_response,  RequestContext         
from django.template import Context
from django.core.paginator import Paginator,QuerySetPaginator
from django.template.loader import get_template
from django.contrib.auth   import    logout,authenticate,login
from django.contrib.auth.views import *
from testapp.forms import *
from django.contrib.auth.decorators import login_required
from django.views.generic import ListView
from testapp.backends import EmailBackend
from testapp.backends import EmailBackend

def main_page(request):
    return render_to_response('main_page.html',{
        'user':request.user
    })

def loggedout(request):
    logout(request)
    return HttpResponseRedirect('/')

def custom_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
    if form.is_valid():
        email = request.POST['email']
        password =  request.POST['password1']

        cb = EmailBackend()
        user = cb.authenticate(email,password)
        # return HttpResponse(user.username)
        if user is not None:
            if user.is_active:
                login(request,user)
                return HttpResponseRedirect('/')
            else:
                return HttpResponse('USer is not Active')
        else:
            return HttpResponse('USer Not found')
else:
    form = LoginForm()
    page_var = RequestContext(request, {
        'form':form, 
    })
    return render_to_response('customlogin.html',page_var)`
</code></pre>

<p>backends.py:</p>

<pre><code>from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend

class EmailBackend(ModelBackend):

    def authenticate(self,email= None,password= None):
        try:
            user = User.objects.get(email=email)

            if user is not None:
                if user.check_password(password):
                    return user
                else :
                    return None
            else:
                return None
        except User.DoesNotExist:
            return None

    def get_user(self,user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None`
</code></pre>

<p>settings.py:</p>

<pre><code>AUTHENTICATION_BACKENDS = ('testapp.backends.EmailBackend','django.contrib.auth.backends.ModelBackend')
</code></pre>

<p>main_page.html:</p>

<pre><code>{% extends "base.html" %}
{% block title %}Welcome to Django Bookmarks{% endblock %}
{% block head %}Welcome to Django Bookmarks{% endblock %}
{% block content %}
{% block customadd %}
    kskdsdsdl;sd
    {% endblock %}
{% if user.username %}
&lt;p&gt;Welcome {{ user.username }}!
Here you can store and share bookmarks!&lt;/p&gt;
{% else %}
&lt;p&gt;Welcome anonymous user!
You need to &lt;a href="/login/"&gt;login&lt;/a&gt;.&lt;/p&gt;
{% endif %}
{% endblock %}
</code></pre>

<p>Now although the main_page.html is called after successfully authentication, In main page <code>{{user.username}}</code> is always <code>None</code> and hence main_page.html opens as anonymous user. </p>

<p><code>user.is_anonymous()</code> in main_page function in view.py returns <code>True</code>. What mistake am I making? After redirect to main_page.html I should see the user as logged in.</p>

<p><strong>Edit 1:</strong> Last time I purposefully didn't put all the packages I included in views.py so I updated it now.</p>