Django——隐藏某些用户的链接
是否可以在 Django Web 应用程序上对那些无权单击链接的用户隐藏某些链接?
我敢打赌,有一种针对每个链接的方法可以检查用户是否有权单击该链接,然后根据该测试显示(或不显示)该链接。然而,当大量链接分布在一大堆网页上时,这可能会非常乏味。有没有什么方法可以通过设置或其他方式在整个网站上实现这一目标?
Is it possible, on a Django webapp, to hide certain links from those users who do not have the permission to click the link?
I bet there is a per-link way to check if the user has persmission to click the link, and then show the link (or not) based on that test. However, when there are a lot links spread across a whole bunch of web pages, that can be ridiculously tedious. Are there any ways to achieve this across the whole website with a setting or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写类似于
spaceless
会检查其内容并删除所有不可访问的链接。这将使您不必手动触摸每个链接。
write a template tag similar to
spaceless
that goes over its contents and removes all links that are not accessible. this would save you from having to touch each link manually.可以编写一个自定义模板标签来接受链接 url,反转它,内省目标视图所需的权限,然后有条件地显示它。
您仍然必须触摸您想要像这样花哨的每个模板中的每个链接,这可能是一个丑陋的野兽。总而言之,如果您想出一种更集中的方式来控制访问,可能会更容易。
It might be possible to write a custom template tag that would accept a link url, reverse it, introspect what permissions were required for the target view, and then conditionally display it.
You'd still have to touch every link in every template that you wanted to make fancy like that, and it would probably be an ugly beast. All in all, it's probably easier if you come up with a more centralized way to control access.