覆盖单一模型或应用的submit_line.html [英] overriding submit_line.html for a single model or app

查看:175
本文介绍了覆盖单一模型或应用的submit_line.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖一个模型或单个应用程序的submit_line.html(可以使用 - 应用程序只有一个模型)。我在文档中看到我无法做到这一点(



另请参阅答案,了解如何确定模板路径的解决方案。


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:

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 %}

解决方案

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.

这篇关于覆盖单一模型或应用的submit_line.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆