允许查看,但不能更改!-Django

允许查看,但不能更改!-Django,第1张

允许查看,但不能更改!-Django

更新 :自Django
2.1起,它已内置。

在admin.py中

# Main reusable Admin class for only viewingclass 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中替换为:

{{ adminform.form.non_field_errors }}

有了这个:

<table>{% for field in adminform.form %}    <tr>      <td>{{ field.label_tag }}:</td><td>{{ field.value }}</td>    </tr>{% endfor %}</table>

然后通过删除下行来删除提交按钮:

{% submit_row %}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5642758.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存