即時通訊使用Django 1.3.1.In我的Django的代碼,我對主網頁下面的模板,其所謂的main_page.html:始終匿名用戶使用Django的代碼時選擇render_to_response
<html>
<head>
<title>My Site</title>
</head>
<body>
<h1>Welcome</h1>
{% if user.username %}
<p>Welcome {{ user.username }}!</p>
{% else %}
<p>Welcome anonymous user!
You need to <a href="/login/">login</a>
{% endif %}
</body>
</html>
我有對此模板的以下視圖:
def main_page(request):
template = get_template('main_page.html')
variables = Context({'user': request.user})
output = template.render(variables)
return HttpResponse(output)
此功能按預期工作,即它檢查用戶是否已經登錄並且相應地打招呼。但是,如果我用下面的代碼替換上述視圖,那麼無論我是否已登錄,我都會在主頁上獲取匿名用戶的消息。
def main_page(request):
return render_to_response(
'main_page.html',
{'user': request.user}
)
這裏可能會出現什麼問題?請幫忙。
謝謝
我看不到任何原因此行爲。我用你的視圖函數創建了一個測試項目,並且不能重現你的問題。你使用的是什麼django版本?檢查你的代碼 - 也許你忘了告訴我們一些重要的東西? –