Cannot hide “Save and add another” button in Django Admin

前端 未结 6 1353
余生分开走
余生分开走 2021-01-03 05:54

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 chang

6条回答
  •  孤城傲影
    2021-01-03 06:43

    I propose (changed option 3 of djvg's answer) removing this html input element with regex as with this example:

    class MyModelAdmin(admin.ModelAdmin):
    
        def add_view(self, request, form_url='', extra_context=None):
            template_response = super(MyModelAdmin, self).add_view(
                request, form_url=form_url, extra_context=extra_context)
            # POST request won't have html response
            if request.method == 'GET':
                # removing Save and add another button: with regex
                template_response.content = re.sub("|>)", "", template_response.rendered_content)
            return template_response
    

    _addanother is this html element's id

提交回复
热议问题