ajax - 覆盖单个模型或应用程序的 submit_line.html

标签 ajax django django-admin

我想为单个模型或单个应用程序覆盖 submit_line.html(两者都可以工作 - 该应用程序只有一个模型)。我在文档中看到我不能这样做( https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#templates-which-may-be-overridden-per-app-or-model )

有什么方法可以测试调用模板的模型或应用程序,以便我可以添加一些条件行为?或者是否有某种方法可以使用不同的模板来代替特定应用程序或模型的 submit_line.html?

mishbah 的回答解决了我最初的问题,但现在我面临另一个问题——当我的代码完成后,会运行一些添加行的东西。我不希望这种情况发生。

这是我想要完成的:

  • 用户点击添加按钮
  • 添加对象页面显示为我的自定义按钮
  • 当我的按钮被点击时,我执行一个 ajax 调用并在添加 div 下方显示结果,并且这个页面会一直显示,直到用户点击一个按钮。

  • 这一切都有效-我唯一的问题是将行添加到数据库中-我想以某种方式防止这种情况发生。

    这是我的代码:

    在主管理页面上,我只有添加按钮:

    enter image description here

    这是我的 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>
    

    {% 结束块 %}

    最佳答案

    可以覆盖 submit-row .只需覆盖 change_form您的模型管理员中的模板:

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

    并在您的自定义 change_form.html ,您需要:
    {% extends "admin/change_form.html" %}
    

    并覆盖 submit_buttons_bottom堵塞:
    {% block submit_buttons_bottom %}
         {# custom submit row goes here #}
    {% endblock %} 
    

    您可以定义自己的自定义 submit_row模板标签,使用原始模板标签作为您的灵感:

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

    另见此 answer有关如何确定模板路径的解决方案。

    关于ajax - 覆盖单个模型或应用程序的 submit_line.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29452826/

    相关文章:

    javascript - Uncaught ReferenceError : $ is not defined in twig template with jQuery import

    javascript - ajax 加载的 html 中的多个 jQuery 插件实例

    craigslist.com 上的 django 子域

    django - 自定义 Django 管理索引页面以显示模型对象

    django - 如何正确使用django内置登录 View

    php - 使用 php ://input and file_put_contents

    python - 不能有多个具有相同选择的模型字段

    gte、lte 的 django_filters 问题

    django - 更好的ArrayField管理小部件?

    javascript - 如何从javascript中的另一个函数中的ajax成功访问变量