Django:for循环中的自定义模板标记错误(Django: custom template tag errors in for loop)

编程入门 行业动态 更新时间:2024-10-26 02:24:33
Django:for循环中的自定义模板标记错误(Django: custom template tag errors in for loop)

我正在尝试使用自定义模板标记,但是当我在for循环中应用标记时,我会遇到一些困难,并且迭代的东西中有多个项目。

我希望标签看起来如下所示,如果我的权限功能评估为True,则只显示文本“WORKED”。

模板代码

{% load permission_tags %} {% for group in groups %} <div>{% permission request.user can_edit_group on group %}WORKED{% endpermission %}</div> {% endfor %}

标签基本上包含用户实例,权限字符串(即“can_edit_group”),“on”关键字(只是为了使语法更好)和一个用于检查权限的对象实例。

我在这里的权限框架我认为工作正常,并不是我的问题的一部分。 我遇到的困难是

"Caught AttributeError while rendering: 'User' object has no attribute 'resolve'" templatetags/permission_tags.py in render, line 23 (I've marked line 23 below)

for循环包含多个组时出现模板错误。 标签只适用于一个组,但如果我添加多个组,则会爆炸。

模板标签在templatetags / permission_tags.py中调用了权限

from django import template register = template.Library() def permission(parser, token): try: tag_name, username, permission, onkeyword, object = token.split_contents() except ValueError: raise template.TemplateSyntaxError("%r tag requires exactly 4 " "arguments" % token.contents.split()[0]) nodelist = parser.parse(('endpermission',)) parser.delete_first_token() return PermissionNode(nodelist, username, permission, object) class PermissionNode(template.Node): def __init__(self, nodelist, user, permission, object): self.nodelist = nodelist self.user = template.Variable(user) self.permission = permission self.object = template.Variable(object) def render(self, context): self.user = self.user.resolve(context) # <---- Line 23 self.object = self.object.resolve(context) # My custom permissions code. I don't think it's # causing the error I am experiencing permissions_obj = self.object.permissions(self.user) content = self.nodelist.render(context) # My custom permissions code. I don't think it's causing # the error. if hasattr(permissions_obj, self.permission): perm_func = getattr(permissions_obj, self.permission) if perm_func(): return content return "" register.tag('permission', permission)

当我在for循环中有多个项目时,你知道为什么这个模板标签会产生错误但是当我只有一个项目时会成功吗?

我还不太了解这个模板标签语法的所有内部工作原理,所以我感觉我在某个地方犯了一个逻辑错误。 任何建议都非常感谢。

谢谢你,乔

I'm trying to get a custom template tag to work but am having some difficulties when I apply the tag within a for loop with multiple items in the thing being iterated across.

I'd like to have the tag look as below and only display the text "WORKED" if my permissions function evaluates to True.

Template code

{% load permission_tags %} {% for group in groups %} <div>{% permission request.user can_edit_group on group %}WORKED{% endpermission %}</div> {% endfor %}

The tag basically takes in a user instance, a permissions string (i.e. "can_edit_group"), an "on" keyword (just to make the syntax nice) and an object instance to check permissions on.

The permissions framework I have going here I think is working ok and is not really part of my question. The difficulty I am having is

"Caught AttributeError while rendering: 'User' object has no attribute 'resolve'" templatetags/permission_tags.py in render, line 23 (I've marked line 23 below)

template error when the for loop contains more than one group. The tag works nicely with just one group, but bombs out if I add more than one.

Template tag called permissions in templatetags/permission_tags.py

from django import template register = template.Library() def permission(parser, token): try: tag_name, username, permission, onkeyword, object = token.split_contents() except ValueError: raise template.TemplateSyntaxError("%r tag requires exactly 4 " "arguments" % token.contents.split()[0]) nodelist = parser.parse(('endpermission',)) parser.delete_first_token() return PermissionNode(nodelist, username, permission, object) class PermissionNode(template.Node): def __init__(self, nodelist, user, permission, object): self.nodelist = nodelist self.user = template.Variable(user) self.permission = permission self.object = template.Variable(object) def render(self, context): self.user = self.user.resolve(context) # <---- Line 23 self.object = self.object.resolve(context) # My custom permissions code. I don't think it's # causing the error I am experiencing permissions_obj = self.object.permissions(self.user) content = self.nodelist.render(context) # My custom permissions code. I don't think it's causing # the error. if hasattr(permissions_obj, self.permission): perm_func = getattr(permissions_obj, self.permission) if perm_func(): return content return "" register.tag('permission', permission)

Do you have any idea why this template tag generates an error when I have more than one item in my for loop but succeeds when I have just one?

I don't quite yet understand all of the inner-workings of this template tags syntax, so I have a feeling I have made a logic error somewhere. Any advice is much appreciated.

Thank you, Joe

最满意答案

当您在此处将self.user更改回User实例时:

self.user = self.user.resolve(context)

它第一次工作,但下一次,因为self.user不再是template.Variable实例,你得到例外: 'User'对象没有属性'resolve'“

一种解决方案是将user和object实例保存在局部变量中:

def render(self, context): user_inst = self.user.resolve(context) object_inst = self.object.resolve(context) permissions_obj = object_inst.permissions(user_inst) content = self.nodelist.render(context) if hasattr(permissions_obj, self.permission): perm_func = getattr(permissions_obj, self.permission) if perm_func(): return content return ""

When you change back self.user to a User instance here:

self.user = self.user.resolve(context)

it works the first time, but the next time, because self.user is no longer a template.Variable instance, you get the exception: 'User' object has no attribute 'resolve'"

One solution is to save user & object instance in local variables:

def render(self, context): user_inst = self.user.resolve(context) object_inst = self.object.resolve(context) permissions_obj = object_inst.permissions(user_inst) content = self.nodelist.render(context) if hasattr(permissions_obj, self.permission): perm_func = getattr(permissions_obj, self.permission) if perm_func(): return content return ""

更多推荐

本文发布于:2023-07-28 18:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   标记   模板   错误   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!