我在Django Admin的change_form.html中添加了一些自定义按钮。 但是,我想向特定用户隐藏这些按钮。
<!-- change_form.html -->
{% extends "admin/change_form.html" %}
{% block submit_buttons_bottom %}
{{ block.super }}
<div class="my-submit-row">
<input type="submit" value="Save as draft" name="_drafted" class="btn btn-lg btn-success">
</div>
{% endblock %}
作为下面的代码,我可以隐藏“ save”和“ save_and_continue”按钮,它们是内置按钮,使它们只能由超级用户显示。
有人对此有任何经验或建议吗?
# admin.py
class MyAdmin(admin.ModelAdmin, ExportCsvMixin):
...
def change_view(self, request, object_id, extra_context=None):
if not request.user.is_superuser:
extra_context = extra_context or {}
extra_context['show_save_and_continue'] = False
extra_context['show_save'] = False
return super(MyAdmin, self).change_view(request, object_id, extra_context=extra_context)