如何删除django1.9中django-admin的保存并继续编辑按钮?

j13ufse2  于 2023-02-10  发布在  Go
关注(0)|答案(3)|浏览(188)

我已经尝试了In Django admin, how can I hide Save and Continue and Save and Add Another buttons on a model admin?中列出的所有解决方案,那篇文章是几年前的,我找不到任何关于如何禁用这些按钮的信息。由于自定义保存,保存并继续编辑按钮会导致错误。在当前版本的django中有什么方法可以禁用它吗?- 不能把应用程序放在django.contrib.admin之前-我只需要禁用一个表单。-我有一个自定义的创建表单,它有一个自定义的保存方法(这是一个帐户创建)

gxwragnw

gxwragnw1#

您可以只隐藏按钮(底层功能仍然存在,但按钮将不可见)。
这个应该可以

from django.contrib import admin

class MyModelAdmin(admin.ModelAdmin):
    ...

    class Media:
        css = {
            'all': ('some/path/to/css/disable_save_and_continue_editing_button.css')
        }
    • 禁用保存并继续编辑按钮. css**
input[name="_continue"] {
  display: none;
}
mf98qq94

mf98qq942#

所以我已经想通了,如果你需要玩这些按钮,那就复制submit_line.html的代码,然后覆盖到你的templates/admin/submit_line.html,转到你的setting.py,然后转到

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),"Project name or app name depends where you put your templates folder","templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

在你的submit_line.html代码中

{% load i18n admin_urls %}
<div class="submit-row">
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save">{% endif %}
{% if show_delete_link %}<p class="deletelink-box"><a href="{% url opts|admin_urlname:'delete' original.pk|admin_urlquote %}"     class="deletelink">{% trans "Delete" %}</a></p>{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" {{ onclick_attrib }}/>{%endif%}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" {{ onclick_attrib }}/>{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" {{ onclick_attrib }}/>{% endif %}
</div>

只需删除savecontinue按钮,或者您可以简单地评论它。希望这会有所帮助。如果有帮助,请将其标记为正确。

9vw9lbht

9vw9lbht3#

要删除**“保存并继续编辑”按钮**,请将**“False”设置为“changform_view()"中的“extra_context['show_save_and_continue']"**,如下所示:

# "admin.py"

from django.contrib import admin
from .models import MyModel

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
        extra_context = extra_context or {}

        extra_context['show_save_and_continue'] = False # Here
        # extra_context['show_save'] = False
        # extra_context['show_delete'] = False

        return super().changeform_view(request, object_id, form_url, extra_context)

您也可以通过将**“False”设置为“show_save_and_continue”in“context.update()"in“render_change_form()"来删除“保存并继续编辑”按钮**,如下所示:

# "admin.py"

from django.contrib import admin
from .models import MyModel

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
        context.update({
            'show_save_and_continue': False, # Here
            # 'show_save': False,
            # 'show_delete': False,
        })
        return super().render_change_form(request, context, add, change, form_url, obj)

相关问题