Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overriding submit_line.html for a single model or app

I want to override submit_line.html for a single model or a single app (either will work - the app has just one model). I see in the docs I cannot do that (https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#templates-which-may-be-overridden-per-app-or-model)

Is there some way to test for what model or app the template is being called for so I can add some conditional behaviour? Or perhaps is there some way to have a different template used in place of submit_line.html for a specific app or model?

mishbah's answer has solved my initial problem, but now I am facing another issue - when my code is done, something runs that add the row. I don't want that to happen.

Here's what I am trying to accomplish:

  1. User clicks add button
  2. Add object page is shown with my custom button
  3. When my button is clicked I execute an ajax call and display the results below the add div, and this page is shown until the user clicks a button.

This all works - my only issue is that a rows get added to the database - I would somehow like to prevent that from happening.

Here is my code:

On the main admin page I have just the add button:

enter image description here

Here is my change_form.html:

{% extends "admin/change_form.html" %}

{% block submit_buttons_bottom %}

<style type="text/css">
    #id_tool_configuration {
        white-space: pre-wrap;
    }   
</style>

<div class="submit-row">
    <input value="Configure" class="default" name="configure" onclick="configureTools(document.getElementById('id_tool_configuration').value); " />
</div>

<script src="/static/scripts/jquery-1.7.js" type="text/javascript"></script>

<script type="text/javascript">
    function configureTools(tcd) {
        var toolConfigData = tcd;
        var request = new XMLHttpRequest();
        var params = 'toolConfigData='+encodeURIComponent(toolConfigData);
        request.open('GET', '{% url 'motor.configuration.views.configure' %}?'+params);
        request.setRequestHeader("Content-type", "text/plain; charset=utf-8");

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                if (request.status == 200) {
                    status = 'Confguration results:';
                }
                else {
                    status = 'Confguration failed';
                }

                $('.submit-row').after(
                    $('<span />')
                    .html('<pre> ' + status + '\n' + request.responseText + '</pre>')
                    .after($('<button />').text('Return').click('function () { return; }'))
                );
            }
        };

        request.send(null);
        return false;
    };
</script>

{% endblock %}

like image 755
Larry Martell Avatar asked Apr 05 '15 00:04

Larry Martell


People also ask

How to override template in django?

You can either put template overrides in your project's templates directory or in an application's templates directory. If you have app and project templates directories that both contain overrides, the default Django template loader will try to load the template from the project-level directory first.

Where is django admin html?

To view the default admin template you can access it in the django/contrib/admin/templates/admin folder.


1 Answers

It is possible to override submit-row. Simply override the change_form template in your modeladmin:

class YourModelAdmin(admin.ModelAdmin):
    change_form_template = 'path/to/custom/change_form.html'

And in your custom change_form.html, you'll need to:

{% extends "admin/change_form.html" %}

and override the submit_buttons_bottom block:

{% block submit_buttons_bottom %}
     {# custom submit row goes here #}
{% endblock %} 

You could define your own custom submit_row templatetag, use the original templatetag for your inspiration:

See https://github.com/django/django/blob/1101467ce0756272a54f4c7bc65c4c335a94111b/django/contrib/admin/templatetags/admin_modify.py#L24

Also see this answer for the solution on how to determine the path of the template.

like image 95
mishbah Avatar answered Nov 15 '22 04:11

mishbah