加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用user.is_authenticated的Django模板标签模板不起作用

发布时间:2020-12-20 11:12:33 所属栏目:Python 来源:网络整理
导读:我有一个奇怪的问题,在我的设置文件中,所有内容都已启用,需要为模板中的user.is_authenticated使用启用. 我有以下模板标记代码: from django import templatefrom progmatic.cms.models import navigation,navigation_itemfrom django.template.defaultfilt
我有一个奇怪的问题,在我的设置文件中,所有内容都已启用,需要为模板中的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 %}

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

提前致谢.

问候,
Bloeper

解决方法

你在 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))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读