How do you populate an update form in a Bootstrap Modal?

917 views
Skip to first unread message

Michael Chenetz

unread,
Mar 3, 2017, 3:10:40 PM3/3/17
to Django users
I am trying to figure out how to populate the update model form in a Bootstrap Modal. I can add to a form no problem but update does not seem to pre-populate the form when it is viewed. I need to understand what i need to do in the view/template to get this done. Any help would be greatly appreciated.

Regards,
Mike

Matthew Pava

unread,
Mar 3, 2017, 3:15:30 PM3/3/17
to django...@googlegroups.com

Hi Mike,

Make sure you are setting the instance argument when you instantiate the form.

Typically, you would pass in a pk to the view.

 

def my_update_view(request, pk):

obj = get_object_or_404(MyModel, pk=pk)

form = MyForm(request.POST or None, instance=obj)

….

 

I hope this helps you.

Best wishes,

Matthew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@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/33b0c293-f017-476d-a6b1-81223f6c863c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Chenetz

unread,
Mar 3, 2017, 3:37:12 PM3/3/17
to Django users
Thanks... I do use the instance variable. I think it has more to do with the modal itself. I see some sites that indicate and ajax call is needed. Not sure what to do there.

Matthew Pava

unread,
Mar 3, 2017, 3:46:28 PM3/3/17
to django...@googlegroups.com

I see.  We are using Bootbox for our modals, but I’m sure the concept is the same.

 

We have a separate template of the form that we include when we initialize the page.  We have an event listener on the modal button, so that when it is clicked, it replaces the HTML inside the form with the HTML returned by our ajax view.  In order to get a specific object, we use the querystring to pass the PK of the object to retrieve in the ajax view.  We have a save button on our modal form, and that POSTs back to the same ajax view.  If it does not validate, we return the HTML of the form with validation errors; otherwise, we send back a success message so that the event handler on the save button can close the modal or reload the page or do something else.

 

That is probably as clear as mud…

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Michael Chenetz


Sent: Friday, March 3, 2017 2:37 PM
To: Django users

--

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Michael Chenetz

unread,
Mar 3, 2017, 9:39:08 PM3/3/17
to Django users
Matt... thanks for that. Do you have sample code you can share. That would be extremely helpful.

Mike

Melvyn Sopacua

unread,
Mar 5, 2017, 10:48:22 AM3/5/17
to django...@googlegroups.com

Hi Michael,

 

Disclaimer: leaving the Javascript side of things out of scope.

 

On Friday 03 March 2017 12:07:16 Michael Chenetz wrote:

> I am trying to figure out how to populate the update model form in a

> Bootstrap Modal. I can add to a form no problem but update does not

> seem to pre-populate the form when it is viewed.

 

Look at how ModelFormMixin does it:

  1. get_content_data from FormMixin calls get_form()
  2. this gets the form class and calls get_form_kwargs()
  3. ModelFormMixin injects the 'instance' keyword argument for the ModelForm

So, in order to prepopulate the form in the modal you need to provide the instance argument and assign it the model instance. I assume you know what model instance you need to update.

--

Melvyn Sopacua

Michael Chenetz

unread,
Mar 5, 2017, 4:40:44 PM3/5/17
to Django users
Thanks Melvyn... I am not having issues with a generic update form. The problem is when using it in a Bootstrap Modal. I think it is a javascript/ajax issue. That is the piece I need to figure out.

Mike

Melvyn Sopacua

unread,
Mar 5, 2017, 5:59:51 PM3/5/17
to django...@googlegroups.com

On Sunday 05 March 2017 13:40:44 Michael Chenetz wrote:

> Thanks Melvyn... I am not having issues with a generic update form.

 

The important stuff is in the last line. I was explaining how you can do it and where to look for inspiration.

 

> The problem is when using it in a Bootstrap Modal. I think it is a

> javascript/ajax issue. That is the piece I need to figure out.

 

That depends on your workflow. You can fill the form using an Ajax call, yes. But - you don't have to.

 

The above is part of code I'm working on. I am actually working on handling duplicated form media in the AdditionalFormsManager, so it certainly isn't production quality yet - but it will work just fine for illustration purposes or base for your own code.

 

--

Melvyn Sopacua

Michael Chenetz

unread,
Mar 6, 2017, 8:43:20 AM3/6/17
to Django users
Melvyn,

Thanks... I will take a look through your code and see if i can figure it out.

Mike
Reply all
Reply to author
Forward
0 new messages