Authenticated但user.is_authenticated仍然为false [英] Authenticated but user.is_authenticated remains false

查看:767
本文介绍了Authenticated但user.is_authenticated仍然为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GAE / Django-nonrel创建简单的应用程序(我不认为问题是特定于GAE或nonrel fork,大多数like PEBKAC是python / django noob,并且会发生在基本的django安装中)

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).

我正在使用django.contrib.auth进行身份验证。

I am using django.contrib.auth for authentication.

在settings.py

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', )

我已经创建了manage.py的超级用户

I've created superuser with manage.py

我已经在其他人使用的模板base.html中遵循{%extends'base.html'%}

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 %} 

.py标准的身份验证(来自django.contrib.auth.forms import AuthenticationForm等)。

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

问题是我可以成功验证,用户名/密码检查正在运行(不能使用不正确的用户/ pwd),并且我在管理页面上进行身份验证 - 但不是在其他页面上进行身份验证 - 或者说我只是用户为null(无)。

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).

我认为 django。 contrib.auth.context_processors.auth 是使这种情况发生的神奇之处,但是在上面显示的settings.py中设置。

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?

编辑(扩展Daniels答案,因为不能做代码格式化在评论中)

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

在views.py我有: -

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})

应该是

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


推荐答案

您是否使用 RequestContext 来渲染你的模板?上下文处理器不适用,除非你这样做。

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

这篇关于Authenticated但user.is_authenticated仍然为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆