twoscoops / two-scoops-of-django-1.11

The issue tracker, changelog, and code repository for Two Scoops of Django 1.11

Home Page:https://www.twoscoopspress.com/products/two-scoops-of-django-1-11

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example 10.13 FlavorForm - missing model instance

bogdal opened this issue · comments

Location within the Book

  • Chapter: 10
  • Section: 6

Description

In the example 10.13, the post method should update the Flavor object, but we don't pass an instance of this object to the FlavorForm. In addition, the redirect url should contain slug from the updated flavor instance.

There is:

...
def post(self, request, *args, **kwargs):
    # Handles updates of the Flavor object
    flavor = get_object_or_404(Flavor, slug=kwargs['slug'])
    form = FlavorForm(request.POST)
    if form.is_valid():
        form.save()
    return redirect("flavors:detail", flavor.slug)

Should be:

...
def post(self, request, *args, **kwargs):
    # Handles updates of the Flavor object
    flavor = get_object_or_404(Flavor, slug=kwargs['slug'])
    form = FlavorForm(request.POST, instance=flavor)
    if form.is_valid():
        flavor = form.save()
    return redirect("flavors:detail", flavor.slug)