Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Lewis Cowles
Lewis Cowles
74,902 Points

Answer: Saving a Model Form

Just wanted to share the answer / solution to this challenge to this challenge as it was brought up by james white that another question by Rohit Gopalan answer was not the solution to the challenge

2 Answers

Lewis Cowles
Lewis Cowles
74,902 Points
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render

from . import forms


def product_form(request):
    form = forms.DigitalProductForm()
    if request.method == "POST":
        form = forms.DigitalProductForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('products:create'))
    return render(request, 'products/product_form.html', {'form': form})

This is simple enough to explain.

1) Firstly you check if the request method is POST (if it is, a form has been submitted to this URL)
2) Next you instantiate a new DigitalProductForm using the POST data from request
3) Because you now have a form populated with the request POST data, you can validate it using the is_valid method on the form instance of DigitalProductForm. We need to check if the form is valid, so rather than assign this result, which we could absolutely do, we just prefix with an if and add a colon before our newline
4) At this point, we know our form is valid, so we save the form instance
5) We want to send the user to the "products:create" endpoint, and the top of the file, suggests we should use reverse as it is importing it. So we return a HttpResponseRedirect which receives the result of the reverse function with the string provided in the challenge notes.

Notes:

  • You start with a form, that has no data, so that you can maintain a consistent view, with as little logic as possible
  • In the eventuality that this methods view receives no form data, or the form is invalid, we want to be able to display the form
  • By not having any else statements we are relying on what we have told the Django Framework to do on success, and in all other cases following a consistent action.

Thanks Lewis for providing a better (more complete answer) to the question.

The original forum thread/post (with a link to the challenge) is here:

https://teamtreehouse.com/community/when-saving-the-django-form-how-can-i-best-use-the-return-httpresponserequest-statement

At this point in time that thread is the only one on the forum for that particular challenge:

https://teamtreehouse.com/community/code-challenge:10972

so I'm glad additional info is available for those that were struggling.

Lewis Cowles
Lewis Cowles
74,902 Points

James, I am pretty sure this will help someone, but until someone asks, I generally would not, and think perhaps it did not need to be done. Esp as I've just upvoted and given myself the best answer, which feels wrong, although leaving a question with an answer and no correct response feels likewise problematic. Thanks for the passion, maybe chill for a bit though?