How to open a popup?

Hi,
In the admin site when creating/updating an object A which has field “fk” which is a foreign key related to an object B (displayed in a select element) you can create/update objects B in a popup window.

I am wondering how to open this new window with Django. I did not find anything about this…

Thanks four your help

Richard

It can’t be done directly by Django. It has to be done using some JavaScript in the browser. (It can be done either directly in JavaScript, or using a library such as jQuery or HTMX.)

In the admin, the green plus sign triggers JavaScript to open a new window for the form.

You can find the code for what the admin is doing in django.contrib.admin.static.admin.js.admin.RelatedObjectLookups.js

Thanks Ken,

I understand the javascript is very generic, allowing to open several popus in case a related object has its own related object.

I am now able to open the popup, but I have troubles regarding how to define the url to redirect to at the end of my creation view (generic CreateView) and to get the select choice up to date…

class Create_framework_agreement(CreateView):
    model = Framework_agreement
    fields=['number', 'company', 'signature_date']

I guess that the view has to handle “_to_field=id” and _“popup=1” variables…

Could you give me some hints?

Thanks

Unfortunately I can’t. For the few cases where I have done such things, I’ve used the modal pop-up technique using jQuery or Bootstrap

Thanks Ken,

I will invstigate this track…

Best regards

Richard

I copy the link content from the browser inspector in the django admin and works for me:

'<a class="related-widget-wrapper-link add-related"'
'id="add_id_manager" data-popup="yes"'
'href="{}">action</a>'

This button open a new window like a edit or add new model with plus or pensil buttons.

1 Like