【问题标题】:'+add' button doesn't work properly when django-ajax-select is used使用 django-ajax-select 时,“+add”按钮无法正常工作
【发布时间】:2020-10-02 12:52:58
【问题描述】:

简而言之:django-ajax-selects 可以很好地过滤现有项目,但通过添加新项目会导致 JS 错误。

详细信息。使用 Django 3.1。 在管理站点,需要使用 ForeignField 创建模型选择对象来对问题建模。 Django-ajax-selects (DAS) 用于填充字段(动态过滤)。通过输入字母,DAS 处理查询集并输出相关问题的列表。可以从列表中选择一个问题并保存新的选择。这一切都很好。

如果没有找到正确的问题,则可以单击+添加按钮并在弹出窗口中添加新问题。 根据 DAS 文档点击“保存”后:

  1. 新问题必须保存到数据库中;
  2. 弹出窗口必须关闭;
  3. 必须用新问题填充已编辑的字段。

Screenshot with popup window

问题是 Django 在第 2 步停止:创建了新问题,弹出窗口变得空白,并且不会在头部出现“弹出关闭...”的情况下关闭。 窗口出现JS错误:

    Uncaught TypeError: window.windowname_to_id is not a function
    at window.dismissAddRelatedObjectPopup (:8000/static/ajax_select/js/ajax_select.js:194)
    at popup_response.js:13

空白页的HTML代码是

<!DOCTYPE html>
<html>
  <head><title>Popup closing…</title></head>
  <body>
    <script id="django-admin-popup-response-constants"
            src="/static/admin/js/popup_response.js"
            data-popup-response="{&quot;value&quot;: &quot;6&quot;, &quot;obj&quot;: &quot;Question object (6)&quot;}">
    </script>
  </body>
</html>

这里有一段来自 ajax_select.js 的 JS 代码,可能会出现错误:

/* Called by the popup create object when it closes.
   * For the popup this is opener.dismissAddRelatedObjectPopup
   * Django implements this in RelatedObjectLookups.js
   * In django >= 1.10 we can rely on input.trigger('change')
   * and avoid this hijacking.
   */

  var djangoDismissAddRelatedObjectPopup = window.dismissAddRelatedObjectPopup || window.dismissAddAnotherPopup;
  window.dismissAddRelatedObjectPopup = function(win, newId, newRepr) {
    // Iff this is an ajax-select input then close the window and
    // trigger didAddPopup
    var name = window.windowname_to_id(win.name);

我在 Django 目录中的任何地方都找不到函数 windowname_to_id(),但是在 djangoprojects.com 的 10 年前的 ticket 中,该函数是为 RelatedObjectLookups.js 提出的:

var uniqueness=(new Date()).valueOf();

function id_to_windowname(text) {
    text = text.replace(/\./g, '__dot__');
    text = text.replace(/\-/g, '__dash__');
    return uniqueness + text;
}

function windowname_to_id(text) {
    text = text.replace(uniqueness, '');
    text = text.replace(/__dot__/g, '.');
    text = text.replace(/__dash__/g, '-');
    return text;
}


我尝试将这些函数几乎没有放入 ajax_select.js,但没有帮助。 如果没有 DAS,+add 按钮可以正常工作。

有解决问题的办法吗? 或者可能有人有一个带有添加功能的 django-ajax-selects 的工作示例?

【问题讨论】:

    标签: javascript django django-ajax-selects


    【解决方案1】:

    问题已由DAS开发者解决,详情见issue

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多