使用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 %} 但它总是返回即使我登录时未登录… 提前致谢. 问候, 解决方法
你在
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)) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python-3.x – 从列表中的元素中删除尾随空格
- python – 使用rest api时选择Azure事件中心的分区
- 我如何阻止特定单元格在Google Colab中运行?
- 什么是在python中确定unicode字符串的解码方法的最佳方法
- python -tf.contrib.learn load_csv_with_header在TensorFl
- python – 模拟Django模型并保存()
- Python中顺序表的实现简单代码分享
- python条件和循环的使用方法
- python – 在外部文件中存储unpicklabe pygame.Surface对象
- matplotlib – iPython notebook:如何防止在imshow()上输出