Search code examples
djangoauthenticationrequestdjango-registrationrequestcontext

django-registration, User missing after login


First at all, I have to say that I've followed a few SO answers (this and this) but I havent solved my problem.

Well, I am using django.contrib.auth.views.login (that works correctly) via django-registration and I have set the next="/home_page/" param in the login form. The view asociated to /home_page/ is excuted, its return looks like this:

return render_to_response('myapp/shop_list.html', 
                        {'shop': entry_list,},
                        context_instance=RequestContext(request))

I didnt render any other template between login and this view but, while debugging, I've realized that the User is not in the request before this return so the template recieves an AnonymousUser and user.is_authenticated() returns False

The user is in the DDBB and the password is correct. How can I get the logged User at this point?

Here is some information about my app that may be helpful:

settings.py
===========

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'myapp',
    'registration',
    'django.contrib.admin',
)

login.html
==========

{% block maincontent %}
<form method="post" action="">{% csrf_token %}
    {{form.as_p}}
    <input name="next" type="hidden" value="/home_page/" />
    <input type="submit" value="Login">
</form>
{% endblock %}

link to login
=============

<a href="{% url django.contrib.auth.views.login %}">Login</a>

Solution

  • The problem was that I created an authentication backend, for another purpose, and the authenticate method wasn't there.