django - 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.
解决方案
为此制作一个 RESTful API 服务是最佳的解决方案。但是您可以通过这样的窗口对象在反应应用程序中使用上下文数据:
在模板中,添加以下代码:
<script>
window.hasPerm = "{{ perms.mygroup }}"
</script>
在反应应用程序中:
{ window.hasPerm && <button onClick=...>
推荐阅读
- python - 编写一个 Python 程序来打印以下数字序列(N 由用户输入):X + X^2/2!+ X^3/3!+... 最多第 N 个学期
- sql-server - 使用 VNET 部署 Azure 数据库部署管道
- python - 如何避免python无关进程之间的忙等待
- anylogic - 如何避免移动代理通过另一个代理而不是避免它?
- python - 使用 struct.pack 在 Python 中打包/解包一个 3 字节整数
- javascript - 单击按钮后显示连续数字的问题
- c# - SignTool 错误:未找到满足所有给定条件的证书。- 如何建造
- python - How to draw on a Fourier transform numpy array Opencv
- python - 从 csv 文件读取变量时 Subprocess.Popen() 不起作用
- javascript - 在 React 中创建标题