Authenticated but user.is_authenticated remains false

我与影子孤独终老i 提交于 2019-12-19 10:17:54

问题


Creating simple app using GAE / Django-nonrel (I don't think the problem is specific to GAE or the nonrel fork, most likey PEBKAC as python/django noob and would occur on basic django installation).

I am using django.contrib.auth for authentication.

In settings.py

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware', )

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.request', )

I've created superuser with manage.py

I've got following in templates base.html which is used in others by {% extends 'base.html' %}

{% if user.is_authenticated %} 
    Hello {{ user.username }} 
    [<a href="{% url django.contrib.auth.views.logout %}">sign out</a>] 
{% else %} 
    [<a href="{% url django.contrib.auth.views.login %}">sign in</a>] 
{% endif %} 

And in urls.py the standard authentication stuff (from django.contrib.auth.forms import AuthenticationForm etc).

Problem is that I can authenticate successfully, username/password checking is working (can't use incorrect user/pwd) and I am authenticated on the admin pages - but not on the other pages - or rather I am but user is null (None).

I think that "django.contrib.auth.context_processors.auth" is the magic that makes this happen but that is setup in settings.py as shown above.

Any tips on how to track this problem down?

EDIT (expanding on Daniels answer as as can't do code formatting in comments)

in views.py I had :-

def detail(request):
    obj = get_object_or_404(MyModel, pk=some_id)    
    return render_to_response('myapp/index.html', {'MyModel': obj})

Should have been

    return render_to_response('myapp/index.html', {'MyModel': obj}, RequestContent(request))

回答1:


Are you using a RequestContext to render your template? Context processors aren't applied unless you do.




回答2:


See django-postman discards RequestContext . With that you're assigning TEMPLATE_CONTEXT_PROCESSORS 2 items, you're overriding the default items.



来源:https://stackoverflow.com/questions/4753413/authenticated-but-user-is-authenticated-remains-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!