Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update view using function based view

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.

like image 661
L.hawes Avatar asked Dec 18 '22 02:12

L.hawes


1 Answers

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})
like image 185
MD. Khairul Basar Avatar answered Dec 20 '22 18:12

MD. Khairul Basar