3

我有一个奇怪的问题,在我的设置文件中,所有需要在模板中启用 user.is_authenticated 的东西都已启用。

我有以下模板标记代码:

from django import template
from progmatic.cms.models import navigation, navigation_item
from django.template.defaultfilters import slugify
from django.shortcuts import render_to_response
from django.template import RequestContext

register = template.Library()

""" Gets the menu items you have entered in the admin.
 No arguments are accpeted"""

def get_hoofd_menu( ):
 menu = navigation.objects.get( slug = "hoofd-menu");
 mcontent = navigation_item.objects.filter( parent_menu = menu);
 return { 'mcontent' : mcontent }

def get_sub_menu( menu ):
 menu = navigation.objects.get( slug = slugify(menu) )
 mcontent = navigation_item.objects.filter( parent_menu = menu )
 c = RequestContext( request, { 'mcontent': mcontent,} )

 return render_to_reponse('menu.html', RequestContext(request, { 'mcontent' : mcontent }) )

register.inclusion_tag('menu.html')( get_hoofd_menu )
register.inclusion_tag('menu.html')( get_sub_menu )

模板(menu.html)如下:

{% block mainmenu %}
    <ul>
     {% for content in mcontent %}
      {% if content.login_required %}
       {% if content.to_page %}
        <li><a href="/page/{{ content.to_page_slug }}">{{ content.name }}</a></li>
       {% endif %}
       {% if content.to_url %}
        {% if content.external %}
         <li><a href="{{ content.to_url }}" rel="external">{{ content.name }}</a></li>
        {% else %}
         <li><a href="{{ content.to_url }}">{{ content.name }}</a></li>
        {% endif %}
       {% endif %}
      {% else %}
       {% if content.to_page %}
        <li><a href="/page/{{ content.to_page_slug }}">{{ content.name }}</a></li>
       {% endif %}
       {% if content.to_url %}
        {% if content.external %}
         <li><a href="{{ content.to_url }}" rel="external">{{ content.name }}</a></li>
        {% else %}
         <li><a href="{{ content.to_url }}">{{ content.name }}</a></li>
        {% endif %}
       {% endif %}
      {% endif %}
     {% endfor %}
    </ul>
    {% if user.is_authenticated %}
    JEEEEEEEJ LOGGED IN
    {% else %}
    Not logged in
    {% endif %}
    {% endblock %}

但它总是返回未登录,即使我登录了......有没有人知道这段代码有什么问题?

提前致谢。

问候,布洛珀

4

5 回答 5

3

你在TEMPLATE_CONTEXT_PROCESSORS设置中有django.core.context_processors.auth吗?

要尝试的另一件事是您的 render_to_reponse 语法。根据文档,它应该是

return render_to_response('my_template.html',
                          my_data_dictionary,
                          context_instance=RequestContext(request))
于 2010-01-17T16:43:36.350 回答
2

您是否注册了任何其他上下文处理器?他们设置了user吗?

我建议在开发服务器下运行并执行以下操作:

rc = RequestContext(request, ... params ...)
user = rc.get('user')
print user.username, user.is_authenticated()

如果你没有得到你期望的值,那么你需要更深入地挖掘。以下应该遍历所有上下文字典并向您显示哪些包含“用户”的值。第一个将是模板看到的内容。

rc = RequestContext(request, ... params ...)
for d in rc:
    print d
于 2010-01-17T17:00:12.417 回答
2

首先感谢所有的帮助。感谢大家,我找到了解决方案:) 结果我需要在每个视图中传递请求对象,并且需要将它作为参数放在我的模板标签中。解决方案代码:

from django import template
from progmatic.cms.models import navigation, navigation_item
from django.template.defaultfilters import slugify
from django.shortcuts import render_to_response
from django.template import RequestContext
from itertools import chain

register = template.Library()

""" Gets the menu items you have entered in the admin.
 No arguments are accepted"""

def get_hoofd_menu( request ):
 menu = navigation.objects.get( slug = "hoofd-menu");
 mcontent = navigation_item.objects.filter( parent_menu = menu, login_required = False);

 if request.user.is_authenticated and not request.user.is_anonymous():
  mmcontent = navigation_item.objects.filter( parent_menu = menu, login_required = True )
 else:
  mmcontent = ""

 final_menu = list(chain(mcontent,mmcontent))

 return { 'mcontent' : final_menu }
 #return render_to_response('menu.html', { 'mcontent' : mcontent } )

def get_sub_menu( request, menu ):
 menu = navigation.objects.get( slug = slugify( menu ) )
 mcontent = navigation_item.objects.filter( parent_menu = menu, login_required = False )
 if request.user.is_authenticated and not request.user.is_anonymous():
  mmcontent = navigation_item.objects.filter( parent_menu = menu, login_required = True )
 else:
  mmcontent = ""

 final_menu = list(chain(mcontent,mmcontent))

 return { 'mcontent' : final_menu }
 #return render_to_response('menu.html', { 'mcontent' : mcontent })

register.inclusion_tag('menu.html')( get_hoofd_menu )
register.inclusion_tag('menu.html')( get_sub_menu )

感谢大家的帮助:)

于 2010-01-17T20:22:23.817 回答
0

你的设置文件中有 django.contrib.auth 和 django.contrib.sessions 吗?

于 2010-01-17T16:21:57.850 回答
0

检查用户是否通过身份验证的实际代码在哪里?如果您在模板中检查它,您应该在将参数从视图传递到模板时提供一些说明。对我来说,当我在顶部执行此操作时它会起作用:

from django.contrib.auth.decorators import login_required

并装饰所有重要的视图,例如:

@login_required
def get_hoofd_menu( ):

概述中都提到了(略高于中途)

于 2010-01-17T16:45:44.643 回答