Django


1900 浏览 6 years, 1 month

7.7 admin 弹出窗口

版权声明: 转载请注明出处 http://www.codingsoho.com/

弹出对话框

在admin的实现,添加,修改等都会弹出新的对话框

它的代码流程如下:

django.contrib.admin.options.py

from django.template.response import SimpleTemplateResponse, TemplateResponse

class ModelAdmin(BaseModelAdmin):
    def response_change(self, request, obj):
        """
        Determines the HttpResponse for the change_view stage.
        """

        if IS_POPUP_VAR in request.POST:
            to_field = request.POST.get(TO_FIELD_VAR)
            attr = str(to_field) if to_field else obj._meta.pk.attname
            # Retrieve the `object_id` from the resolved pattern arguments.
            value = request.resolver_match.args[0]
            new_value = obj.serializable_value(attr)
            return SimpleTemplateResponse('admin/popup_response.html', {
                'action': 'change',
                'value': escape(value),
                'obj': escapejs(obj),
                'new_value': escape(new_value),
            })

django.contrib.admin.templates.admin.popup_response.html

{% load i18n %}<!DOCTYPE html>
<html>
  <head><title>{% trans 'Popup closing...' %}</title></head>
  <body>
    <script type="text/javascript">
      {% if action == 'change' %}
        opener.dismissChangeRelatedObjectPopup(window, "{{ value|escapejs }}", "{{ obj|escapejs }}", "{{ new_value|escapejs }}");
      {% elif action == 'delete' %}
        opener.dismissDeleteRelatedObjectPopup(window, "{{ value|escapejs }}");
      {% else %}
        opener.dismissAddRelatedObjectPopup(window, "{{ value|escapejs }}", "{{ obj|escapejs }}");
      {% endif %}
    </script>
  </body>
</html>

admin.js.admin.RelatedObjectLookup.js

function dismissChangeRelatedObjectPopup(win, objId, newRepr, newId) {
    objId = html_unescape(objId);
    newRepr = html_unescape(newRepr);
    var id = windowname_to_id(win.name).replace(/^edit_/, '');
    var selectsSelector = interpolate('#%s, #%s_from, #%s_to', [id, id, id]);
    var selects = django.jQuery(selectsSelector);
    selects.find('option').each(function() {
        if (this.value == objId) {
            this.innerHTML = newRepr;
            this.value = newId;
        }
    });
    win.close();
};

触发弹出窗口

不确定是不是下面两个函数,下次加个断点试一下

django.contrib.admin.templates.admin.change_form.html

{% block admin_change_form_document_ready %}
    <script type="text/javascript">
        (function($) {
            $(document).ready(function() {
                $('.add-another').click(function(e) {
                    e.preventDefault();
                    var event = $.Event('django:add-another-related');
                    $(this).trigger(event);
                    if (!event.isDefaultPrevented()) {
                        showAddAnotherPopup(this);
                    }
                });
                $('.related-lookup').click(function(e) {
                    e.preventDefault();
                    var event = $.Event('django:lookup-related');
                    $(this).trigger(event);
                    if (!event.isDefaultPrevented()) {
                        showRelatedObjectLookupPopup(this);
                    }
                });
                $('body').on('click', '.related-widget-wrapper-link', function(e) {
                    e.preventDefault();
                    if (this.href) {
                        var event = $.Event('django:show-related', {href: this.href});
                        $(this).trigger(event);
                        if (!event.isDefaultPrevented()) {
                            showRelatedObjectPopup(this);
                        }
                    }
                });
                $('body').on('change', '.related-widget-wrapper select', function(e) {
                    var event = $.Event('django:update-related');
                    $(this).trigger(event);
                    if (!event.isDefaultPrevented()) {
                        updateRelatedObjectLinks(this);
                    }
                });
                $('.related-widget-wrapper select').trigger('change');
            {% if adminform and add %}
                $('form#{{ opts.model_name }}_form :input:visible:enabled:first').focus()
            {% endif %}
            });
        })(django.jQuery);
    </script>
{% endblock %}
function showRelatedObjectPopup(triggeringLink) {
    var name = triggeringLink.id.replace(/^(change|add|delete)_/, '');
    name = id_to_windowname(name);
    var href = triggeringLink.href;
    var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
    win.focus();
    return false;
}