1

djangoサイトのユーザー認証に問題があります。動作しているように見えるログイン画面があります。ユーザーがログインをクリックすると、に電話をかけますが、django.contrib.auth.login 正常に機能しているようです。ただし、後続のページでは、ユーザーがログインしていることを認識していません。例 {% user.is_authenticated %}は誤りです。my-accountやなど、ログインしているユーザーが利用できるようにしたいメニュー機能もいくつかありますlogout。これらの機能は、ログインページ以外ではご利用いただけません。これは本当に奇妙です。

これはユーザーコンテキストの問題のようです。しかし、ログインが安定していることを確認するために、どのようにコンテキストを渡すことになっているのかわかりません。 Does anyone know at could be going on here? Any advice?

---------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))
4

3 に答える 3

3

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]
于 2011-10-17T18:04:21.143 に答える
2

RequestContextを使用します。

于 2011-10-17T16:39:32.733 に答える
1

Danielが提案したように、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)) 
于 2011-10-17T17:46:07.820 に答える