Django admin model add_view: how to remove "save and add more" buttons?
I was able to remove the Save and Add More and Save and Continue Editing buttons by running 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 use my change_view, but when I add 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 strange 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 am using django-guardian and I am wondering if this is how this is causing my problem? Does anyone know how to get rid of those annoying buttons from the submit_line part of the page when adding a new model instance?
source to share
If you want to hide these buttons directly for cosmetic purposes, you can also use CSS and this may not be the best approach as you can turn them back on by checking the css, this is of course simple and still granular enough to hide them for some 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 above path.
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 can change between django versions for these buttons, now I am using Django 1.6.6 and I don't think they have changed recently. If you want this to be effective throughout the admin site, you can copy the default admin / base_site.html template into your static directory and overwrite the "extrahead" block to include this style. See base_site.html .
Hope the CSS approach helps :) This will definitely not throw errors for you.
source to share