Recently I had to modify django admin page massively, while trying to add a new button at add new model item at admin page I got into trouble, trouble was not to show the button, or get that button working but it was at variable passing. So in this blog I am going to describe, how did I solve it.
I am overriding this template submit_line.html:
{% load i18n admin_urls %}{% if show_save %}{% endif %} {% if show_save_as_draft %} {% endif %} {% if show_save_and_add_another %}{% endif %} {% if show_save_and_continue %}{% endif %} {% if show_delete_link %} {% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %} {% trans "Delete" %} {% endif %}
Here,
{{show_save_as_draft}}
is at out extra context of our ModelAdmin while it is showing:
#/home/sadaf2605/PycharmProjects/stripe/stripe/news/admin.py class ArticleAdmin(admin.ModelAdmin): change_form_template = 'admin/news/change_form.html' def change_view(self, request,object_id, form_url='', extra_context=None): extra_context = extra_context or {} extra_context["show_save_as_draft"] = True return super(ArticleAdmin, self).change_view(request,object_id, form_url, extra_context)
Still
{{show_save_as_draft}}
is not showing up. This is the problem
To solve this problem I actually override a template tag that was responsible for showing buttons, basically that template tag was only keeping few selected context field. In this new tag I am keeping tags which are necessary for my app.
#stripe/stripe/news/templatetags/stripe_admin_tag.py __author__ = 'sadaf2605' from django import template register = template.Library() from django.contrib.admin.templatetags import admin_modify @register.inclusion_tag('admin/submit_line.html', takes_context=True) def submit_line_row(context): context = context or {} ctx= admin_modify.submit_row(context) if "show_save_as_draft" in context.keys(): ctx["show_save_as_draft"] = context["show_save_as_draft"] return ctx
and then finally I need to override change_form.html as well, I need to replace:
{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
with:
{% load stripe_admin_tag %} {% block submit_buttons_bottom %}{% submit_ine_row %}{% endblock %}
/stripe/stripe/stripe/templates/admin/news/change_form.html
{% extends "admin/base_site.html" %} {% load i18n admin_urls admin_static admin_modify %} {% block extrahead %}{{ block.super }} {{ media }} {% endblock %} {% block extrastyle %}{{ block.super }}{% endblock %} {% block coltype %}colM{% endblock %} {% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-form{% endblock %} {% if not is_popup %} {% block breadcrumbs %}{% endblock %} {% endif %} {% block content %}{% block object-tools %} {% if change %}{% if not is_popup %}{% endblock %}{% block object-tools-items %}
{% endif %}{% endif %} {% endblock %}- {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %} {% trans "History" %}
{% if has_absolute_url %}- {% trans "View on site" %}
{% endif%} {% endblock %}