[Answer]-How to edit a form using modelForm Based Using Django

0👍

The best would be using django’s generic class based views:

If you’d still won’t to roll your own, you should use the instance keyword, e.g:

suggestion = Suggestion.objects.get(pk=pk)
form = SuggestionForm(instance=suggestion)

You should capture the pk in the url pattern, and make sure your function expects it:

def suggestion(request, pk=None):
    if request.method == "POST":
    ...

Still, the generic class based views do all the hard work for you, so not sure why you would do it manually (learning the mechanism could be one reason).

1👍

Django comes with built in views that handle Creating, updating and deleteing objects using django’s forms.

Using UpdateView should allow you to update an object using your ModelForm,

using updateview you have to specify the objects id in the url and tell the view which kwarg to look for the id in. By default I think it uses pk

Leave a comment