How to remove the save and continue editing button in django-admin in django 1.9?
Question:
I have attempted all the solutions listed in In Django admin, how can I hide Save and Continue and Save and Add Another buttons on a model admin? and that post was from a few years ago, and I can’t find anything about how to disable those buttons. Due to custom saving, the Save And Continue Editing button causes an error. Is there any way to disable it in the current version of django?
Constraints:
– Cannot put app before django.contrib.admin
– I need to only disable it for one form.
– I have a custom form for creation, and it has a custom save method (it’s an account creation)
Answers:
You can just hide the button (the underlying functionality will still be there, but the button will not be visible).
This should work
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
...
class Media:
css = {
'all': ('some/path/to/css/disable_save_and_continue_editing_button.css')
}
disable_save_and_continue_editing_button.css
input[name="_continue"] {
display: none;
}
SO I have figured it out. If you need to play with these buttons then copy the code of submit_line.html
and then override it to your templates/admin/submit_line.html
and go to your setting.py
and then go to
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',
],
},
},
]
And in yours submit_line.html
code
{% 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>
Just remove that save
and continue
buttons or you can simply comment it.
Hope this will help.
Mark it correct if it helps.
To remove "Save and continue editing" button, set "False" to "extra_context[‘show_save_and_continue’]" in "changeform_view()" as shown below:
# "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)
You can also remove "Save and continue editing" button by setting "False" to "show_save_and_continue" in "context.update()" in "render_change_form()" as shown below:
# "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)
I have attempted all the solutions listed in In Django admin, how can I hide Save and Continue and Save and Add Another buttons on a model admin? and that post was from a few years ago, and I can’t find anything about how to disable those buttons. Due to custom saving, the Save And Continue Editing button causes an error. Is there any way to disable it in the current version of django?
Constraints:
– Cannot put app before django.contrib.admin
– I need to only disable it for one form.
– I have a custom form for creation, and it has a custom save method (it’s an account creation)
You can just hide the button (the underlying functionality will still be there, but the button will not be visible).
This should work
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
...
class Media:
css = {
'all': ('some/path/to/css/disable_save_and_continue_editing_button.css')
}
disable_save_and_continue_editing_button.css
input[name="_continue"] {
display: none;
}
SO I have figured it out. If you need to play with these buttons then copy the code of submit_line.html
and then override it to your templates/admin/submit_line.html
and go to your setting.py
and then go to
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',
],
},
},
]
And in yours submit_line.html
code
{% 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>
Just remove that save
and continue
buttons or you can simply comment it.
Hope this will help.
Mark it correct if it helps.
To remove "Save and continue editing" button, set "False" to "extra_context[‘show_save_and_continue’]" in "changeform_view()" as shown below:
# "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)
You can also remove "Save and continue editing" button by setting "False" to "show_save_and_continue" in "context.update()" in "render_change_form()" as shown below:
# "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)