質問

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>
役に立ちましたか?

解決 2

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

他のヒント

Really, the only thing that could cause something like that is not having cookies enabled in your browser, or cookies otherwise not being stored.

When you login, a session is created, the logged in user is added to the session and a session cookie is delivered to the client (your browser). On the next request, your browser will send the cookie back to the server, which uses it to pull the matching session out of the database and populate request.user with the user that was logged in (simplified explanation here, of course).

So, really the only way for request.user to be an AnonymousUser is to have either not logged in or not sent the cookie to the server.

Check the settings in your browser as well as any extensions you may be running to make sure nothing is interfering with cookies. You might want to try another browser, and see if the behavior is repeatable.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top