2👍
For documentation purposes I’ve created a more complete version of @Radishx’s solution. This will add a publish button in QuestionAdmin
.
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['backend/templates'], # Search in backend app first to overwrite admin
'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',
],
},
},
]
backend/templates/admin/submit_line.html
{% load i18n admin_urls %}
<div class="submit-row">
{% block submit-row %}
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save">{% endif %}
{% if show_delete_link %}
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
<p class="deletelink-box"><a href="{% add_preserved_filters delete_url %}" class="deletelink">{% trans "Delete" %}</a></p>
{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% if can_change %}{% trans 'Save and continue editing' %}{% else %}{% trans 'Save and view' %}{% endif %}" name="_continue">{% endif %}
{% if show_publish %}<input type="submit" value="{% trans 'Publish' %}" class="default" name="_publish">{% endif %}
{% if show_close %}<a href="{% url opts|admin_urlname:'changelist' %}" class="closelink">{% trans 'Close' %}</a>{% endif %}
{% endblock %}
</div>
Notice the {% if show_publish %}
.
backend/admin.py
@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, form_url='', extra_context=None):
try:
extra_context['show_publish'] = True
except TypeError:
extra_context = {'show_publish': True}
return self.changeform_view(request, object_id, form_url, extra_context)
Change backend
to your app name.
After this, you have to make sure that Django knows what to do with a _publish button.
1👍
1.copy the /your_python_path/python_version/site-packages/lib/django/crontab/admin/template
to your project and add TEMPLATE config to your settings
(u can also edit it in your venv without copy to your proj)
2.then edit your_proj/template/admin/submit_line.html
,add/modify some <input>/<button>
as you wish
e.x. <button class="default" onclick="xxxx" > sth </button>
- [Django]-Django view returning count of users based on last_login
- [Django]-Why is Postgres query faster than Redis query?
- [Django]-How to use toggle switch with django boolean field?
0👍
In order to add a new button to an admin template(ie. admin/change_form.html), you have to override the admin template and add a custom submit_buttons_bottom
block. A template tag is used to render the default submit buttons, you can add your custom button and styling here.
{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}