使用 GAE / Django-nonrel 创建简单的应用程序(我不认为问题特定于 GAE 或 nonrel 分支,最有可能是 PEBKAC 作为 python/django noob 并且会在基本 django 安装时发生)。
我正在使用 django.contrib.auth 进行身份验证。
在设置.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创建了超级用户
我在模板 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 import 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))