无法隐藏“保存并添加另一个"Django Admin 中的按钮 [英] Cannot hide "Save and add another" button in Django Admin

查看:17
本文介绍了无法隐藏“保存并添加另一个"Django Admin 中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当满足某些条件时,对于特定模型,我想隐藏 Django 管理员更改表单中的所有保存"按钮.因此,我覆盖了相关 ModelAdmin 中的 changeform_view 方法,如下所示:

I would like to hide all the "Save" buttons in Django's Admin's Change Form, for a specific model, when certain conditions are met. Therefore, I override the changeform_view method in the relevant ModelAdmin, like so:

def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
    extra_context = extra_context or {}
    obj = collection_management_MammalianLine.objects.get(pk=object_id)
    if obj:
        if not (request.user.is_superuser or request.user.groups.filter(name='Lab manager').exists() or request.user == obj.created_by):
            extra_context['show_save'] = False
            extra_context['show_save_and_continue'] = False
            extra_context['show_save_and_add_another'] = False
        else:
            pass
    else:
        pass
    return super(MammalianLinePage, self).changeform_view(request, object_id, extra_context=extra_context)

使用此代码,我可以成功隐藏保存"和保存并继续"按钮,但无法隐藏保存并添加另一个"按钮.我可以看到 submit_line.html 包含以下三行

With this code, I can successfully hide the "Save" and "Save and continue" buttons, but not the "Save and add another" one. I can see that submit_line.html contains the following three lines

{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" />{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" />{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" />{% endif %}

我的问题是:为什么我可以隐藏保存"和保存并继续"按钮,而不能隐藏保存并添加另一个"按钮?即使相关的模板标签 (show_save_and_continue) 在模板中.

My question is: why can I hide the "Save" and "Save and continue" buttons, but not the "Save and add another" one? Even though the relevant templatetag (show_save_and_continue) is in the template.

推荐答案

在传递的上下文中检查除show_save_and_continue 之外的其他键.Django 总是设置这个直接.

The other keys are checked for in the passed context except show_save_and_continue. Django always sets this directly.

'show_save_and_add_another': (
        context['has_add_permission'] and not is_popup and
        (not save_as or context['add'])
    ),

您可以修补 submit_row 模板标签函数以首先检查 context 字典中的 show_save_and_add_another.

You can patch the submit_row template tag function to first check the context dictionary for show_save_and_add_another.

@register.inclusion_tag('admin/submit_line.html', takes_context=True)
def submit_row(context):
    """
    Display the row of buttons for delete and save.
    """
    change = context['change']
    is_popup = context['is_popup']
    save_as = context['save_as']
    show_save = context.get('show_save', True)
    show_save_and_continue = context.get('show_save_and_continue', True)
    show_save_and_add_another = context.get('show_save_and_add_another', False)
    ctx = Context(context)
    ctx.update({
        'show_delete_link': (
            not is_popup and context['has_delete_permission'] and
            change and context.get('show_delete', True)
        ),
        'show_save_as_new': not is_popup and change and save_as,
        'show_save_and_add_another': (
            context.get('show_save_and_add_another', None) or
            (context['has_add_permission'] and not is_popup and
            (not save_as or context['add']))
        ),
        'show_save_and_continue': not is_popup and context['has_change_permission'] and show_save_and_continue,
        'show_save': show_save,
    })
    return ctx

编辑

修补admin/submit_line.html"包含标签的步骤

  1. models.pyviews.py

templatetags文件夹

复制django/contrib/admin/templatetags/admin_modify.pytemplatetags/admin_modify.py.

用上面的函数定义覆盖submit_row.

Overwrite submit_row function definition with the one above.

请注意这适用于 Django 2.0 及以下版本.

Note that this is applicable for Django 2.0 and below.

对于最近的 Django 版本,找到允许该表达式为 False 的上下文组合.例如

For recent Django versions, find a context mix that allows this expression to be False.e.g.

has_add_permission and not is_popup and
(not save_as or add) and can_save

见上面表达式中使用的名称的值.

这篇关于无法隐藏“保存并添加另一个"Django Admin 中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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