许可查看,但不改变! - Django [英] Permission to view, but not to change! - Django

查看:99
本文介绍了许可查看,但不改变! - 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.

推荐答案

在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 %}

这篇关于许可查看,但不改变! - Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆