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 trialDavid Axelrod
36,073 Pointsdouble form creation
would it effect django's route performance if we put the blank form logic after the request.post check? If it's posted then the server shouldn't have to create the blank forms. unless those forms are necessary. Thoughts?
something like this
From this
def quiz_create(request, course_pk):
course = get_object_or_404(models.Course, pk = course_pk)
form = forms.QuizForm()
if request.method == 'POST':
form = forms.QuizForm(request.POST)
if form.is_valid():
quiz = form.save(commit=False) # dont put into db
quiz.course = course
quiz.save()
messages.add_message(request, messages.SUCCESS, "Quiz added!!!")
return HttpResponseRedirect(quiz.get_absolute_url())
return render(request, 'courses/quiz_form.html',{'form': form})
To this
def quiz_create(request, course_pk):
#moved course into post logic
#form is right before render()
if request.method == 'POST':
form = forms.QuizForm(request.POST)
if form.is_valid():
quiz = form.save(commit=False) # dont put into db
course = get_object_or_404(models.Course, pk = course_pk)
quiz.course = course
quiz.save()
messages.add_message(request, messages.SUCCESS, "Quiz added!!!")
return HttpResponseRedirect(quiz.get_absolute_url())
form = forms.QuizForm()
return render(request, 'courses/quiz_form.html',{'form': form})
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsIn the original flow, if the posted form is not valid, form
will be left set to the posted form values when the page is rendered in the return statement. This gives the user feedback on what failed during validation.
In your proposed flow, if the form is not valid, the form is reset before the render statement losing the user's entered data.
It might work if the form reset was in an else
block:
def quiz_create(request, course_pk):
#moved course into post logic
#form is right before render()
if request.method == 'POST':
form = forms.QuizForm(request.POST)
if form.is_valid():
quiz = form.save(commit=False) # dont put into db
course = get_object_or_404(models.Course, pk = course_pk)
quiz.course = course
quiz.save()
messages.add_message(request, messages.SUCCESS, "Quiz added!!!")
return HttpResponseRedirect(quiz.get_absolute_url())
else: # not POST
form = forms.QuizForm()
return render(request, 'courses/quiz_form.html',{'form': form})
David Axelrod
36,073 PointsDavid Axelrod
36,073 PointsAhhh awesome! Thanks for the explanation