Django 中的用户上下文
我的 django 站点的用户身份验证遇到问题。我的登录屏幕似乎可以工作。当用户单击登录时,我调用 django.contrib.auth.login ,它似乎工作正常。但是在后续页面上不知道有用户登录。示例 {% user.is_authenticated %}
为 false。我还希望登录用户可以使用一些菜单功能,例如 my-account
和 logout
。除登录页面外,这些功能不可用。这真的很奇怪。
这似乎是一个用户上下文问题。但我不确定我应该如何传递上下文以确保我的登录稳定。 有人知道这里会发生什么吗?有什么建议吗?
---------base.html的一部分------------
<!--- The following doesn't register even though I know I'm authenticated -->
{% if user.is_authenticated %}
<div id="menu">
<ul>
<li><a href="/clist">My Customers</a></li>
<li><a href="#">Customer Actions</a></li>
<li><a href="#">My Account</a></li>
</ul>
</div>
{% endif %}
---------我的views.py ---- -------------
# Should I be doing something to pass the user context here
def customer_list(request):
customer_list = Customer.objects.all().order_by('lastName')[:5]
c = Context({
'customer_list': customer_list,
})
t = loader.get_template(template)
return HttpResponse(t.render(cxt))
I am having problems with user authentication for my django site. I have a log-in screen that seems to work. When the user clicks log-in, I call the django.contrib.auth.login
and it seems to work fine. However on subsequent pages have no knowledge that there is a user logged in. Example {% user.is_authenticated %}
is false. There are also some menu functions that I want to be available for logged in users such as my-account
and logout
. Those functions are not available, except on the log-in page. Which is really strange.
This seems to be a user context problem. But I'm not sure how I am supposed to be passing a context around to ensure that my login is stable. Does anyone know at could be going on here? Any advice?
---------part of base.html------------
<!--- The following doesn't register even though I know I'm authenticated -->
{% if user.is_authenticated %}
<div id="menu">
<ul>
<li><a href="/clist">My Customers</a></li>
<li><a href="#">Customer Actions</a></li>
<li><a href="#">My Account</a></li>
</ul>
</div>
{% endif %}
---------my views.py -----------------
# Should I be doing something to pass the user context here
def customer_list(request):
customer_list = Customer.objects.all().order_by('lastName')[:5]
c = Context({
'customer_list': customer_list,
})
t = loader.get_template(template)
return HttpResponse(t.render(cxt))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如丹尼尔建议的那样,使用 RequestContext...或者更好,只需使用 render_to_response 快捷方式:
As Daniel Suggested, use the RequestContext... or better, just use the render_to_response shortcut:
如果您使用的是 Django 1.3,则可以使用
render()
快捷方式,它会自动为您包含RequestContext
。在这种情况下,您可以更进一步,使用通用
ListView
:If you're using Django 1.3, you can use the
render()
shortcut, which automatically includesRequestContext
for you.In this case, you could go one step further, and use the generic
ListView
:使用 RequestContext。
Use a RequestContext.