Django 中的用户上下文

发布于 2024-12-10 18:52:58 字数 1237 浏览 0 评论 0原文

我的 django 站点的用户身份验证遇到问题。我的登录屏幕似乎可以工作。当用户单击登录时,我调用 django.contrib.auth.login ,它似乎工作正常。但是在后续页面上不知道有用户登录。示例 {% user.is_authenticated %} 为 false。我还希望登录用户可以使用一些菜单功能,例如 my-accountlogout。除登录页面外,这些功能不可用。这真的很奇怪。

这似乎是一个用户上下文问题。但我不确定我应该如何传递上下文以确保我的登录稳定。 有人知道这里会发生什么吗?有什么建议吗?

---------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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独陪着我 2024-12-17 18:52:59

正如丹尼尔建议的那样,使用 RequestContext...或者更好,只需使用 render_to_response 快捷方式:

from django.template import RequestContext
from django.shortcuts import render_to_response

def customer_list(request):
   customer_list = Customer.objects.all().order_by('lastName')[:5]
   return render_to_response(
         "path_to/template.html",
         {'customer_list':customer_list,}, 
         context_instance=RequestContext(request)) 

As Daniel Suggested, use the RequestContext... or better, just use the render_to_response shortcut:

from django.template import RequestContext
from django.shortcuts import render_to_response

def customer_list(request):
   customer_list = Customer.objects.all().order_by('lastName')[:5]
   return render_to_response(
         "path_to/template.html",
         {'customer_list':customer_list,}, 
         context_instance=RequestContext(request)) 
月亮是我掰弯的 2024-12-17 18:52:58

如果您使用的是 Django 1.3,则可以使用 render() 快捷方式,它会自动为您包含 RequestContext

from django.shortcuts import render

def customer_list(request):
   customer_list = Customer.objects.all().order_by('lastName')[:5]
   return render(request, "path_to/template.html",
                 {'customer_list': customer_list,})

在这种情况下,您可以更进一步,使用通用 ListView

from django.views.generic import ListView

class CustomerList(Listview):
    template_name = 'path_to/template.html'
    queryset = Customer.objects.all().order_by('lastName')[:5]

If you're using Django 1.3, you can use the render() shortcut, which automatically includes RequestContext for you.

from django.shortcuts import render

def customer_list(request):
   customer_list = Customer.objects.all().order_by('lastName')[:5]
   return render(request, "path_to/template.html",
                 {'customer_list': customer_list,})

In this case, you could go one step further, and use the generic ListView:

from django.views.generic import ListView

class CustomerList(Listview):
    template_name = 'path_to/template.html'
    queryset = Customer.objects.all().order_by('lastName')[:5]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文