Opened 17 years ago

Closed 17 years ago

#6307 closed (duplicate)

Creating new related object through popup interface in admin can produce JavaScript error

Reported by: Webchemist Owned by: nobody
Component: contrib.admin Version: newforms-admin
Severity: Keywords: popup
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I got an JavaScript error when trying to create an related object through popup window in django admin

Django version: 0.97-newforms-admin-SVN-6991
Error: text.replace is not a function
Source: http://127.0.0.1:8000/admin-media/js/admin/RelatedObjectLookups.js
Line: 6

This error is produced in this function:

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 = win.name.replace(/___/g, '.');
    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') {
            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();
}

It is need to change the lines (call toString() method for every entering value, because here a string expected):

    ....
    newId = html_unescape(newId.toString());
    newRepr = html_unescape(newRepr.toString());
    ....

Attachments (1)

js.diff (986 bytes ) - added by Jonas <django@…> 17 years ago.
Patch for django/contrib/admin/options.py

Download all attachments as: .zip

Change History (6)

comment:1 by Simon Greenhill <dev@…>, 17 years ago

Triage Stage: UnreviewedAccepted

by Jonas <django@…>, 17 years ago

Attachment: js.diff added

Patch for django/contrib/admin/options.py

comment:2 by Jonas <django@…>, 17 years ago

Owner: changed from nobody to anonymous
Status: newassigned

I ran into this as well and solved it a bit differently. I think it's cleaner and more in line with the comments surrounding the affected code if all primary keys get passed as strings. See attached patch.

comment:3 by anonymous, 17 years ago

Owner: changed from anonymous to nobody
Status: assignednew

comment:4 by Antti Kaihola, 17 years ago

I can confirm that this bug affects 0.97-newforms-admin-SVN-7192 and that the patch from Jonas fixes it.

comment:5 by Gary Wilson, 17 years ago

Resolution: duplicate
Status: newclosed

dup of #6100, which was fixed in [7194].

Note: See TracTickets for help on using tickets.
Back to Top