Skip to content

Commit

Permalink
Use instance feature of ModelForm
Browse files Browse the repository at this point in the history
  • Loading branch information
wsot committed Sep 19, 2022
1 parent f0363b9 commit cc42faf
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/community_db/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,12 @@ def detail_person_with_template(request, pk):
def edit_person_with_template(request, pk):
person = get_object_or_404(Person, id=pk)
if request.POST:
form = PersonForm(request.POST)
form = PersonForm(request.POST, instance=person)
if form.is_valid():
person.first_name = form.cleaned_data["first_name"]
person.last_name = form.cleaned_data["last_name"]
person.country = form.cleaned_data["country"]
person.mobile_number = form.cleaned_data["mobile_number"]
person.save()
form.save()
return HttpResponseRedirect(reverse("fbv-person-detail", args=[person.id]))
else:
form = PersonForm(
{
"first_name": person.first_name,
"last_name": person.last_name,
"country": person.country,
"mobile_number": person.mobile_number,
}
)
form = PersonForm(instance=person)
context = {"object": person, "form": form}
return render(request, "community_db/person_form_in_base.html", context)

Expand Down

0 comments on commit cc42faf

Please sign in to comment.