简体   繁体   English

使用 django-ajax-select 时,“+add”按钮无法正常工作

[英]'+add' button doesn't work properly when django-ajax-select is used

In short: django-ajax-selects works fine with filtering existing items, but gives JS error by adding new item.简而言之:django-ajax-selects 可以很好地过滤现有项目,但通过添加新项目会导致 JS 错误。

Details.细节。 Using Django 3.1.使用 Django 3.1。 At Admin site one needs to create object of model Choice with ForeignField to model Question.在管理站点,需要使用 ForeignField 创建模型选择的对象来建模问题。 Django-ajax-selects ( DAS ) is used to populate the field (dynamic filtering). Django-ajax-selects ( DAS ) 用于填充字段(动态过滤)。 By typing letters DAS handles the queryset and outputs the list of relevant questions.通过输入字母 DAS 处理查询集并输出相关问题的列表。 One can choose a question from the list and save new Choice.可以从列表中选择一个问题并保存新的选择。 All this works fine.所有这些工作正常。

If no proper questions by typing are found then one can click +add button and add new question in popup window with form.如果通过输入找不到合适的问题,则可以单击 + 添加按钮并在带有表单的弹出窗口中添加新问题。 After clicking 'Save' according to DAS docs:根据 DAS 文档单击“保存”后:

  1. new question must be saved into the database;新问题必须保存到数据库中;
  2. popup window must be closed;弹出窗口必须关闭;
  3. the edited field must be populated with new question .编辑的字段必须填充新问题。

Screenshot with popup window带有弹出窗口的屏幕截图

The problem is that Django stops at step2: new question is created, the popup window is getting blank and doesn't close with "Popup closing ..." in the head.问题是 Django 在第 2 步停止:创建了新问题,弹出窗口变得空白并且不会以“弹出窗口关闭...”关闭。 There is an JS error in the window:窗口出现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

The HTML-code of the blank page is空白页的 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>

Here is a piece of the JS code from ajax_select.js, where the error probably appears:下面是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);

I couldn't find the function windowname_to_id() anywhere in Django dirs, but in the 10-years old ticket at djangoprojects.com, where this function was proposed for RelatedObjectLookups.js:我在 Django 目录中的任何地方都找不到函数 windowname_to_id(),但是在 djangoprojects.com 上的 10 年旧中,该函数被提议用于 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;
}


I've tried to put barely these functions into ajax_select.js, but it didn't help.我试图将这些函数几乎放到 ajax_select.js 中,但没有帮助。 Without DAS the +add button works fine.如果没有 DAS,+add 按钮工作正常。

Any idea to solve the problem?有解决问题的想法吗? Or may be anyone has a working example with django-ajax-selects with add function?或者可能有人有一个带有添加功能的 django-ajax-selects 的工作示例?

该问题已由 DAS 开发人员 解决,详情请参阅issue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM