I have a registration form that when the user enters in valid registration data in it, it logs the user in and redirects the user to another form to enter more data. This new form contains more data about the user and is created using the ModelForm class below:
class UserProfileAddForm(ModelForm):
class Meta:
model = UserProfile
exclude = ('user')
and the UserProfile model looks like:
class UserProfile(models.Model):
GRADE_YEAR_CHOICES = (
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate')
)
school = models.CharField(max_length=64)
grade_year = models.CharField(max_length=2, choices=GRADE_YEAR_CHOICES)
gpa = models.DecimalField(decimal_places=2, max_digits=6, blank=True, null=True)
user = models.ForeignKey(User, unique=True)
Where the user field is a ForeignKey to Django's User object. I am having a hard time setting the user field to the user making the request. Here is what I have tried in my views.py
First attempt:
def more(request):
if request.method == 'POST':
form = UserProfileAddForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.user = request.user
form.save()
return HttpResponseRedirect('/register/success/')
else:
form = UserProfileAddForm()
variables = RequestContext(request, {
'form': form
Second attempt:
def more(request):
if request.method == 'POST':
form = UserProfileAddForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.user = User.objects.filter(username=request.user)
form.save()
return HttpResponseRedirect('/register/success/')
else:
form = UserProfileAddForm()
variables = RequestContext(request, {
'form': form
})
return render_to_response('registration/more.html', variables)
Either way I try it does not seem to save the data to the database. It will however redirect me to the success page as though it did save successfully.