我徒劳地尝试创建一个简单的 Django 模板标签来显示或隐藏我网站上提交的评论旁边的“删除”链接。
简而言之,我想将评论对象传递给模板标签,确定当前登录的用户是否有权删除评论,然后显示或不显示链接。
我的模板中的用法如下:
{% load access_tags %}
{% if_authorized comment %}
<a href="{% url delete_comment comment.id %}">Delete</a>
{% endif_authorized %}
请放心,如果用户有权删除评论,我还会检查相应的视图。
这种类型的标签有具体的名称吗?如果是的话,它肯定会对我的谷歌搜索有所帮助。感谢您的帮助!
更新 1:
根据我网站的工作方式,两个人可能有权删除评论:1) 评论创建者和 2) 留下评论的帖子的所有者。因此,我需要根据评论确定是否存在这些条件之一。
我不认为我可以使用 Django 的内置权限系统之类的东西,因为它要求权限“按对象类型全局设置,而不是按特定对象实例设置”。
就我而言,用户“Bob”可能有权删除评论(如果他写了评论或评论位于他创建的帖子上),但也可能不允许他删除评论(如果他正在查看对某人的评论)别人的帖子)。
更新2:
看来您无法将对象传递给模板标签,只能传递字符串:“虽然您可以使用 token.split_contents() 将任意数量的参数传递给模板标签,但参数都被解包为字符串字面意思。”我想我会传递相关评论对象的 ID 并将其拉入标签中。
我对此是错误的,只需访问传入的对象,例如:
self.comment.resolve(context).user
vs.
self.comment.user
I'm trying, in vain, to create a simple Django template tag to either show or hide a "delete" link next to a submitted comment on my site.
In a nutshell, I want to pass the comment object to the template tag, determine if the currently logged in user is authorized to delete the comment and then either show or not show the link.
The usage in my template would be like so:
{% load access_tags %}
{% if_authorized comment %}
<a href="{% url delete_comment comment.id %}">Delete</a>
{% endif_authorized %}
Rest assured that I also check in the appropriate view if the user is authorized to delete the comment.
Does this type of tag have a specific name? It would certainly help me with my Google searches if it did. Thanks for your help!
UPDATE 1:
The way my site works, two people are potentially authorized to delete a comment: 1) the comment creator and 2) the owner of the post where the comment was left. Because of this, I need to determine, per comment, if one of those conditions is present.
I don't think I can use something like Django's built-in permission sytem, since it requires that permissions "be set globally per type of object, no per specific object instance".
In my case, user "Bob" may have permissions to delete a comment (if he wrote it or it is on a post he created), but he also may not be allowed to delete it (if he is looking at a comment on someone else's post).
UPDATE 2:
It appears that you can't pass objects to a template tag, only strings: "Although you can pass any number of arguments to a template tag using token.split_contents(), the arguments are all unpacked as string literals." I guess I'll pass the id of the comment object in question and pull it in the tag.
I was wrong about this, just have to access the passed in object like:
self.comment.resolve(context).user
vs.
self.comment.user
发布评论
评论(3)
好的,我就是这样做的...
标签在模板中的使用方式如下:
模板标签文件名为“access_tag.py”,位于我的应用程序的“templatetags”目录中。这是“access_tag.py”的内容:
完成。最后,使用内置的 {% if %} 标签来进行此比较是非常容易的,但由于我还有其他每个对象的授权要做,所以我将继续构建这些自定义“访问标签”。另外,模板代码看起来更加整洁:)
OK, this is how I did it...
The tag is used like this in the template:
The template tag file is called "access_tag.py" and is in my app's "templatetags" directory. This is the contents of "access_tag.py":
Done. In the end, it would have been pretty easy to just use the built-in {% if %} tag to do this comparison, but since I'll have other per-object authorizations to do, I will continue to build out these custom "access_tags". Plus, the template code looks so much tidier :)
已经存在一个旨在做您想做的事情的项目。
django-authority 允许对模板中的权限进行细粒度控制。
Django 1.2 还在模板中包含用户权限。
There already exists a project that aims to do what you would like to do.
django-authority allows for fine grain control over permissions in templates.
Django 1.2 also contains user permissions in templates, too.
怎么样...创建一个自定义标签 在上下文中写入一个变量,然后使用
{% if %}
测试该变量,就像这样:
当然是“check_access”标签将在上下文中写入“has_access”。
祝你好运
how about this... create a custom tag that writes a variable in the context, then test that variable using
{% if %}
it'd be something like this:
of course the "check_access" tag would write the "has_access" in the context.
Good Luck