Add model item with AJAX pop up

101 views
Skip to first unread message

guettli

unread,
Feb 6, 2016, 8:12:48 AM2/6/16
to Django users

I use django-select2 for easy ForeignKey picking. Works nice.


But how can I add a new item in the ForeignKey table?


I would like to have a big [+] button near the select2 field and then a (ajax) modal dialog should open (like in the admin interface).


AFAIK django-select2 does not help here. Is there a different library which can help here?


I would like to re-use and not to re-invent. Maybe I was blind, but I could not

find a matching comparison grid on djangopackages.com.


I would like to avoid a "old-school" browser-window-pop-up :-)


Regards,

  Thomas Güttler

guettli

unread,
Feb 6, 2016, 3:36:44 PM2/6/16
to Django users
The result should look like the "create user" modal dialog on this page:

https://jqueryui.com/dialog/#modal-form

Any idea how to get this done?

Up to now I use the usual pattern:

 - redirect after post, if the submitted data is valid.
 - show form again with error message if the data is not valid.

I could code this myself. But I ask myself it is good to do so.

I guess this has already been solved many times before. I most cases
there is a library/reusable-app. But here I found none.

Regards,
  Thomas Güttler

pa xapy

unread,
Feb 8, 2016, 3:17:48 AM2/8/16
to Django users
hi
easiest way is to write separate view which will handle ajax requests (get and post) and render html (as a json field) and success status as response.
on frontend you will handle this response to show modal with form and submit it until model will successfully saved
once it happens, you can refetch your users list in select field

but I don't know any application which implement all this stack. I think you should handle this modal by yourself

guettli

unread,
Feb 8, 2016, 3:12:47 PM2/8/16
to Django users
There many auto-complete fields. Adding an item in a popup is needed very often. The django-admin does it, too.

For me it is strange that there is no reusable widget for this.

guettli

unread,
Mar 3, 2016, 9:24:39 AM3/3/16
to Django users
The comparison grid of autocomplete inputs has a feature called "Add-another outside the admin":

These libraries support adding new instances. I don't know if they use an ajay pop-up or an other solution:

https://www.djangopackages.com/grids/g/auto-complete/
Reply all
Reply to author
Forward
0 new messages