Opened 8 years ago

Closed 7 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: UI/UX:

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@…> 8 years ago.
Patch for django/contrib/admin/options.py

Download all attachments as: .zip

Change History (6)

comment:1 Changed 8 years ago by Simon Greenhill <dev@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Changed 8 years ago by Jonas <django@…>

Patch for django/contrib/admin/options.py

comment:2 Changed 8 years ago by Jonas <django@…>

  • Owner changed from nobody to anonymous
  • Status changed from new to assigned

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 Changed 8 years ago by anonymous

  • Owner changed from anonymous to nobody
  • Status changed from assigned to new

comment:4 Changed 8 years ago by akaihola

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

comment:5 Changed 7 years ago by gwilson

  • Resolution set to duplicate
  • Status changed from new to closed

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

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