admin popup + button help

210 views
Skip to first unread message

Giuseppe

unread,
Apr 20, 2016, 5:06:22 PM4/20/16
to Django users
I am trying to get a pop up that will let me create a new object to associate to a many to many relationship on a form/model. In the same way that it works in django-admin.

I have been followed the instructions from here, and its not working for me. 

whenever I click the plus button it opens the related object creating form in the same tab instead of a new pop up. When I try to submit the new object I get a cert error and I included the cert tag in the template.

Let me know what code you want to see and I can post it. Also if you know of a different resource, or library or way of doing this that would be amazing.

By the way I think the link provided was for django 1.1 and im using django 1.9.1

Giuseppe

unread,
Apr 27, 2016, 2:37:19 PM4/27/16
to Django users
I managed to get this working, sort of. I learned that I need to include jquery.init.js to my parent form. otherwise It wont open in a new window. Now the only issue I am encountering is after I submit my child form, the window goes white, but does not close. I think the code that gets called when I submit the form is this 
return HttpResponse('<script type="text/javascript">opener.dismissAddRelatedObjectPopup(window, "%s", "%s");</script>' % \
(escape(newObject._get_pk_val()), escape(newObject)))
I cant figure out if its being called at all or if its being called on the child or if its being called on the parent and thats why its not closing. "window" is being staticaly typed so its not in the view code other than that one spot. 

Here is the Django code for the dismiss function (this was pulled directly from  https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js
function dismissAddRelatedObjectPopup(win, newId, newRepr) {
       var name = windowname_to_id(win.name);
       var elem = document.getElementById(name);
       if (elem) {
           var elemName = elem.nodeName.toUpperCase();
           if (elemName === 'SELECT') {
               elem.options[elem.options.length] = new Option(newRepr, newId, true, true);
           } else if (elemName === 'INPUT') {
               if (elem.className.indexOf('vManyToManyRawIdAdminField') !== -1 && elem.value) {
                   elem.value += ',' + newId;
               } else {
                   elem.value = newId;
               }
           }
           // Trigger a change event to update related links if required.
           $(elem).trigger('change');
       } else {
           var toId = name + "_to";
           var o = new Option(newRepr, newId);
           SelectBox.add_to_cache(toId, o);
           SelectBox.redisplay(toId);
       }
       win.close();
   }

I can see that after I submit my new object gets selected in the dropdown inside the parent form, but the popup window from the child form does not close. Any ideas how to get the child form to close, the dismiss function should be doing that correct?

David

unread,
Oct 14, 2016, 9:22:12 AM10/14/16
to Django users
Hi Giuseppe,
I was upgrading to a later version of django and ran into the same issue. Did you find a work-around?

Derek

unread,
Oct 20, 2016, 5:46:40 AM10/20/16
to Django users
The article linked to is from 2008 - have some things not changed in Django since then?

David Ogutu

unread,
Oct 20, 2016, 3:59:24 PM10/20/16
to django...@googlegroups.com
Actually, never mind, I was able to work around the problem be redefining hurry.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/2XIgnW4sEHk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/45d0262c-fd2c-4b65-9353-0e7194fe2e0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages