使用 GAE/Django-nonrel 创建简单的应用程序(我不认为该问题特定于 GAE 或 nonrel 分支,最有可能是 PEBKAC 作为 python/django noob 并且会在基本 django 安装时发生)。
我使用 django.contrib.auth 进行身份验证。
在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创建了 super 用户
我在模板 base.html 中有以下内容,其他人使用了 {% 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 %}
在 urls.py 中包含标准身份验证内容(从 django.contrib.auth.forms 导入 AuthenticationForm 等)。
问题是我可以成功进行身份验证,用户名/密码检查正在工作(不能使用不正确的用户/密码)并且我在管理页面上进行了身份验证 - 但不在其他页面上进行了身份验证 - 或者更确切地说,我是但用户是空(无)。
我认为“django.contrib.auth.context_processors.auth ”是实现这种情况的魔法,但这是在 settings.py 中设置的,如上所示。
有关如何追踪此问题的任何提示?
编辑(扩展丹尼尔斯的答案,因为无法在注释中进行代码格式化)
在views.py中我有:-
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渲染你的模板?除非您这样做,否则不会应用上下文处理器。
关于python - 已通过身份验证,但 user.is_authenticated 仍为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4753413/