How can I pass an object into a model form to pre-populate the field when the page is rendered? I want to do something similar to the build in Django UpdateView class based view but with a function based view.
Just get the object from model and pass that object as instance to the form. Then pass the form to the template. Write your view like below example.
def func(request, id):
object = Model.objects.get(id=id)
form = ModelForm(instance=object)
return render(request, 'my_template.html', {'form':form})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With