2011-01-20 13 views
2

使用GAE/Django-nonrel創建簡單的應用程序(我認爲問題不是特定於GAE或nonrel fork,最類似於PEBKAC作爲python/django noob並且會發生在基本django上安裝)。已驗證,但user.is_authenticated保持爲假

我使用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

我在這是在他人所用的模板base.html文件得到了以下{%「延伸的基礎。 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設置如上圖所示的魔力。

有關如何跟蹤這個問題下任何提示?

EDIT(擴大對丹尼爾斯的答案是不能做的代碼中的註釋格式)在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)) 

回答

4

您是否在使用RequestContext來呈現您的模板?上下文處理器不適用,除非你這樣做。

+0

doh ...它在那裏,在「注意 - 是的,請閱讀這裏,在這裏」;) – Ryan

+2

我也有這個問題:正確登錄後,我重定向到主頁,這個視圖返回render_to_response(' MYAPP/shop_list.html」,{ '店':entry_list,},context_instance = RequestContext的(要求))但是,在這一點上,request.user是AnonymusUser所以當主頁模板負荷,user.is_authenticated永遠是假的。我錯過了什麼? – juankysmith

+0

救了我,雖然鏈接不工作了,在這裏看到:https://docs.djangoproject.com/en/1.9/ref/templates/api/#django.template.RequestContext – platzhersh

相關問題