首页 > 解决方案 > Django Group Permissions in React

问题描述

In Django I have added a group and can see it on my template by using:

{{ perms.mygroup }}

I have a React front end app which has a number of links to navigate around the front end but the 'Edit' link must only be visible if the user has the 'mygroup' permission stated above.

How is it that I pass the server permission to the front end app? Am I missing something simple here as I can't find out how to do it. I've read about using React's new context API but the examples I've seen are setting the context on the front end, rather than bringing in a variable from the template.

标签: djangoreactjs

解决方案


为此制作一个 RESTful API 服务是最佳的解决方案。但是您可以通过这样的窗口对象在反应应用程序中使用上下文数据:

在模板中,添加以下代码:

<script>
window.hasPerm = "{{ perms.mygroup }}"
</script>

在反应应用程序中:

{ window.hasPerm && <button onClick=...>

推荐阅读