[Django]-Django admin model add_view: how to remove "save and add another" buttons?

5๐Ÿ‘

โœ…

If you want to hide these buttons plainly for cosmetic purposes you can also use CSS and it might not be the best approach since you can enable them back by inspecting the css, it certainly is simple and still granular enough to only hide them on certain model admins.

admin.py:

class MyModelAdmin(admin.ModelAdmin)

    ....

    class Media:
        #js = ('' )  # Can include js if needed
        css = {'all': ('my_admin/css/my_model.css', )}  

my_model.css is located in the static files folder in the path above.

my_model.css:

/* Optionally make the continue and save button look like primary */
input[name="_continue"]{
    border: 2px solid #5b80b2;
    background: #7CA0C7;
    color: white;
}

/* Hide the "Delete", "Add Another" and "Save" buttons, customize this to what you need  */
.deletelink, input[name="_addanother"], input[name="_save"]{
    display: none;
}

The classes and names may change between django versions for these buttons, I am using Django 1.6.6 now and I donโ€™t think they have changed recently. If you want this to be effective on your entire admin site, you can copy admin/base_site.html default template into your static dir and overwrite the โ€˜extraheadโ€™ block to include this style. See base_site.html.

Hopefully the CSS approach helps ๐Ÿ™‚ It certainly will not cause any errors for you.

๐Ÿ‘คradtek

3๐Ÿ‘

I have this code working in a small project, I do not put references because I deduced it and it works ๐Ÿ™‚

##admin.py
class Admincommits(admin.ModelAdmin):
    list_display = ["id"]
    list_filter = ["id"]
    search_fields = ["id"]
    actions = None

    def add_view(self, request, extra_context=None):
        if request.user.is_superuser:
            extra_context = extra_context or {}
            extra_context['show_save_and_continue'] = False
            #extra_context['show_save_and_add_another'] = False
            #extra_context['show_save'] = False
            #extra_context['show_delete'] = False
        return super(Admincommits, self).add_view(request, extra_context=extra_context) 
๐Ÿ‘คArelis_xzx

Leave a comment