Permission to view, but not to change! - Django(允许查看,但不能更改!- 姜戈)
问题描述
是否可以授予用户查看权限,但不能更改或删除.
is it possible to give users the permission to view, but not to change or delete.
目前我看到的唯一权限是添加"、更改"和删除"......但那里没有读取/查看".
currently in the only permissions I see are "add", "change" and "delete"... but there is no "read/view" in there.
我真的需要这个,因为有些用户只能查看管理面板,才能查看添加的内容.
I really need this as some users will only be able to consult the admin panel, in order to see what has been added in.
推荐答案
更新:自 Django 2.1 现在是内置的.
Update: Since Django 2.1 this is now built-in.
在 admin.py 中
In admin.py
# Main reusable Admin class for only viewing
class ViewAdmin(admin.ModelAdmin):
"""
Custom made change_form template just for viewing purposes
You need to copy this from /django/contrib/admin/templates/admin/change_form.html
And then put that in your template folder that is specified in the
settings.TEMPLATE_DIR
"""
change_form_template = 'view_form.html'
# Remove the delete Admin Action for this Model
actions = None
def has_add_permission(self, request):
return False
def has_delete_permission(self, request, obj=None):
return False
def save_model(self, request, obj, form, change):
#Return nothing to make sure user can't update any data
pass
# Example usage:
class SomeAdmin(ViewAdmin):
# put your admin stuff here
# or use pass
在 change_form.html 中替换:
In change_form.html replace this:
{{ adminform.form.non_field_errors }}
用这个:
<table>
{% for field in adminform.form %}
<tr>
<td>{{ field.label_tag }}:</td><td>{{ field.value }}</td>
</tr>
{% endfor %}
</table>
然后通过删除这一行来移除提交按钮:
Then remove the submit button by deleting this row:
{% submit_row %}
这篇关于允许查看,但不能更改!- 姜戈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:允许查看,但不能更改!- 姜戈
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01