本文介绍了Django Admin - RelatedObjectLookups - 如何在父窗口中刷新并设置选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



具体来说,我想要我的一种表单像管理页面那样工作,所以我想我会看看代码,看看它是如何工作的。希望用户能够点击选择列表旁边的+图标,并将其带到管理页面的弹出窗体中添加新项目。



当他们在这里输入一个新项目,我希望该新项目出现在选择框中,并被选中(就像这个功能在管理页面上如何运作)。



I将管理员js库复制到我自己的模板中,我使我的链接调用相同的JS函数,并且弹出窗口确实打开,但是在我保存一个新对象后,弹出窗口变为空,而不是关闭,父对象没有任何反应



这是我在我的网页上的内容:

 。 .. 
< td>
< div class =fieldWrapper>
< select name =form-0-plasmidid =id_form-0-plasmid>
...
< / select>
< a href =/ admin / VirusTracker / plasmid / add /class =add-anotherid =add_id_plasmidonclick =return showAddAnotherPopup(this);> < img src =/ media / admin / img / admin / icon_addlink.gifwidth =10height =10alt =Add Another/>< / a>
< / div>
< / td>
...

我尝试通过管理表单上的javascript查看它是如何工作,但我没有看到任何关闭窗口或填充父窗口的选择。



提前感谢任何帮助。



更新3



在运行dismissAddAnotherPopup时,我收到此JavaScript错误

 SelectBox未定义

在dismissAddAnotherPopup中指向此行

  SelectBox.add_to_cache(toId,o); 

我以为我知道Javascript,但是我看不到该变量应该来自哪里: - (



更新2



点击保存在弹出窗口我得到一个空白页面,这是该页面的源代码:

 < script type =文本/ javascript> opener.dismissAddAnotherPopup(窗口,9,CMV_flex_myr_GENE1 _._._ WPRE_BGH);< / script> 

所以似乎这个javascript没有执行或失败。



更新 p>

这是Daniel提到的相关代码,所以唯一的问题是这个代码没有被触发,或是不正确地触发。



django / contrib / admin / options.py:

  ... 
如果请求。 POST.has_key(_ popup):
return HttpResponse('< script type =text / javascript> op ener.dismissAddAnotherPopup(window,%s,%s);< / script>'%\
#escape()调用force_unicode。
(escape(pk_value),escapejs(obj))
...

/media/admin/js/admin/RelatedObjectLookups.js:

  function dismissAddAnotherPopup(win,newId,newRepr){
// newId和newRepr预计以前由
// django.utils.html.escape转义。
newId = html_unescape(newId);
newRepr = html_unescape(newRepr);
var name = windowname_to_id(win.name);
var elem = document.getElementById(name);
if(elem){
if(elem.nodeName =='SELECT'){
var o = new Option(newRepr,newId);
elem.options [elem.options.length] = o;
o.selected = true;
} else if(elem.nodeName =='INPUT'){
if(elem.className.indexOf('vManyToManyRawIdAdminField')!= -1&& elem.value){
elem.value + =','+ newId;
} else {
elem.value = newId;
}
}
} else {
var toId = name +_to;
elem = document.getElementById(toId);
var o = new Option(newRepr,newId);
SelectBox.add_to_cache(toId,o);
SelectBox.redisplay(toId);
}
win.close();
}


解决方案

启动元素的id属性来标识要更新的选择字段。 (从beginig中删除'add_')。



所以我只是改变了链接的id属性来匹配我的模板中的select元素的id:

 < a href =/ admin / VirusTracker / plasmid / add /class =add-anotherid =add_id _ {{field.html_name }}onclick =return showAddAnotherPopup(this);> < img src =/ media / admin / img / admin / icon_addlink.gifwidth =10height =10alt =Add Another/>< / a> 

哇我希望这已经记录在某个地方!我失去了几个小时。



(有关更全面的技术细节,请参阅我的更新问题。)


I want one of my forms to work just like the admin page does so I figured I'd look in the code and see how it works.

Specifically I want the user to be able to click a "+" icon next to a select list and be taken to the admin page's popup form to add a new item.

When they enter a new item there, I want that new item to appear in the select box, and be selected (Just like how this feature works on the admin pages).

I copied the admin js libraries into my own template, and I made my link call the same JS function and the popup windows does open correctly, but after I save a new object the popup window goes blank instead of closing, and nothing happens on the parent page.

Here's what I put in my page:

...
<td>
    <div class="fieldWrapper">
        <select name="form-0-plasmid" id="id_form-0-plasmid">
        ...
        </select>
        <a href="/admin/VirusTracker/plasmid/add/" class="add-another" id="add_id_plasmid" onclick="return showAddAnotherPopup(this);"> <img src="/media/admin/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a>
    </div>
</td>
...

I tried stepping through the javascript on the admin form to see how it's working, but I'm not seeing anything that would close the window or populate the parent window's select.

Thanks in advance for any help.

Update 3

I'm getting this javascript error when dismissAddAnotherPopup is run

"SelectBox is not defined"

Which is pointing to this line in dismissAddAnotherPopup

SelectBox.add_to_cache(toId, o);

I thought I knew Javascript, but I don't see where that variable is supposed to come from :-(

Update 2

Everything seems to be firing properly. After I click save on the popup window I get a blank page. This is the source of that page:

<script type="text/javascript">opener.dismissAddAnotherPopup(window, "9", "CMV_flex_myr_GENE1_._._WPRE_BGH");</script>

So it would seem that this javascript isn't being executed or is failing.

Update

Here is the relevant code that Daniel mentioned. So the only problem is that this code either isn't firing, or is firing incorrectly.

django/contrib/admin/options.py:

...
        if request.POST.has_key("_popup"):
            return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
                # escape() calls force_unicode.
                (escape(pk_value), escapejs(obj)))
...

/media/admin/js/admin/RelatedObjectLookups.js:

function dismissAddAnotherPopup(win, newId, newRepr) {
    // newId and newRepr are expected to have previously been escaped by
    // django.utils.html.escape.
    newId = html_unescape(newId);
    newRepr = html_unescape(newRepr);
    var name = windowname_to_id(win.name);
    var elem = document.getElementById(name);
    if (elem) {
        if (elem.nodeName == 'SELECT') {
            var o = new Option(newRepr, newId);
            elem.options[elem.options.length] = o;
            o.selected = true;
        } else if (elem.nodeName == 'INPUT') {
            if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
                elem.value += ',' + newId;
            } else {
                elem.value = newId;
            }
        }
    } else {
        var toId = name + "_to";
        elem = document.getElementById(toId);
        var o = new Option(newRepr, newId);
        SelectBox.add_to_cache(toId, o);
        SelectBox.redisplay(toId);
    }
    win.close();
}
解决方案

Ok, the javascript simply uses the id attribute of the launching element to identify the select field to update. (after removing 'add_' from the beginig).

So I simply changed the link's id attribute to match the select element's id in my template:

<a href="/admin/VirusTracker/plasmid/add/" class="add-another" id="add_id_{{field.html_name}}" onclick="return showAddAnotherPopup(this);"> <img src="/media/admin/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a>

Wow I wish this had been documented somewhere! I lost a few hours on this.

(See my updates to the question for more technical details on how it all works.)

这篇关于Django Admin - RelatedObjectLookups - 如何在父窗口中刷新并设置选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:15