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 trialHeidi Ulrich
4,624 Pointsform_valid goes wrong
I took what was necessary from the preceding video, but now it says Task 1 is no longer passing. Always a killer warning because it says nothing. I guess I have a typo?
from django.contrib.auth import login
from django.core.urlresolvers import reverse_lazy
from django.views import generic
from . import forms
class SignUp(generic.CreateView):
form_class = forms.UserCreateForm
template_name = 'accounts/signup.html'
success_url = reverse_lazy('products:list')
def form_valid(self, form):
login(self.request, form.get_user())
return super().form_valid(form)
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsYou're started in the right direction. The method form_valid
needs to return a valid form. But the form needs to validated to get at the form data. The solution is to call super()
first, then save the result for the return
statement.
The necessary steps are:
- call super form to validate for, hold result to return later
res = super().form_valid(form)
- Get
username
andpassword1
from theform.cleaned_data
dictionary - authenticate user with
username
andpassword1
and assign touser
- login user using
login(self.request, user)
- return
res
from above
Post back if you need more help. Good Luck