I was able to remove the "save and add another" and "save and continue editing" buttons by implementing the following code:
# At the start of my admin.py file I have:
from django.contrib.admin.templatetags.admin_modify import *
from django.contrib.admin.templatetags.admin_modify import submit_row as original_submit_row
@register.inclusion_tag('admin/submit_line.html', takes_context=True)
def submit_row(context):
ctx = original_submit_row(context)
ctx.update({
'show_save_and_add_another': context.get('show_save_and_add_another', ctx['show_save_and_add_another']),
'show_save_and_continue': context.get('show_save_and_continue', ctx['show_save_and_continue'])
})
return ctx
class MyModelAdmin(GuardedModelAdmin):
# Then inside MyModelAdmin I have this:
def change_view(self, request, object_id, form_url='', extra_context=None):
extra_context = extra_context or {}
extra_context['show_save_and_add_another'] = False
extra_context['show_save_and_continue'] = False
return super(MyModelAdmin, self).change_view(request, object_id,
form_url, extra_context=extra_context)
This works great when I'm using my change_view, but when I'm adding a new instance of the model, the buttons reappear. I tried the following:
def add_view(self, request, form_url='', extra_context=None):
extra_context = extra_context or {}
extra_context['show_save_and_add_another'] = False
extra_context['show_save_and_continue'] = False
return super(MyModelAdmin, self).add_view(self, request, form_url='', extra_context=extra_context)
But it gives me a bizarre MissingAtrribute error -- here's the traceback:
Traceback:
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/contrib/admin/options.py" in wrapper
432. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/contrib/admin/sites.py" in inner
198. return view(request, *args, **kwargs)
File "/home/username/Development/MyProject/webapp/MyModel/admin.py" in add_view
153. return super(MyModelAdmin, self).add_view(self, request, form_url='', extra_context=extra_context)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapped_view
95. result = middleware.process_view(request, view_func, args, kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/middleware/csrf.py" in process_view
111. request.COOKIES[settings.CSRF_COOKIE_NAME])
Exception Type: AttributeError at /admin/MyModel/ModelInstance/add/
Exception Value: 'MyModelAdmin' object has no attribute 'COOKIES'
I'm using django-guardian and wondering if this is somehow causing my problem? Does anyone know how to get rid of these annoying buttons from the submit_line part of the template when adding a new model instance?
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.