Re: More User/Profile troubles
Ah that did it. The other thing I was doing wrong, the reason all the different things i was trying didnt get me anywhere- I hadnt run syncdb when I should of. That stupid mistake wasted a lot of time.. Its taken a while but it appears to be working perfectly now! Thanks for your help. Phil On 4 Dec, 18:17, Brian Neal <[EMAIL PROTECTED]> wrote: > On Dec 4, 11:08 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > wrote:> I apologise for spamming everyone, here i stuck it up at dpaste with > > the comments which makes it a bit clearer-http://dpaste.com/96128/ > > You are almost there. You are assigning a form instance to your custom > profile's user foreign key. That is not what you want. You want to > assign the appropriate user instance to that foreign key field. > > Instead of: > extraformedit.user = userformInstance > > Try this: > extraformedit.user = request.user > or > extraformedit.user = userforminstance.user > > Another suggestion: you probably need to check to see if both forms > are valid before going ahead with the saving. > > if form.is_valid() and extraform.is_valid(): > > I recently did something similiar, I found my code and dpasted it. In > my case I am showing the view for a user to edit her existing profile. > I hope it helps:http://dpaste.com/96150/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: More User/Profile troubles
On Dec 4, 11:08 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > I apologise for spamming everyone, here i stuck it up at dpaste with > the comments which makes it a bit clearer-http://dpaste.com/96128/ > You are almost there. You are assigning a form instance to your custom profile's user foreign key. That is not what you want. You want to assign the appropriate user instance to that foreign key field. Instead of: extraformedit.user = userformInstance Try this: extraformedit.user = request.user or extraformedit.user = userforminstance.user Another suggestion: you probably need to check to see if both forms are valid before going ahead with the saving. if form.is_valid() and extraform.is_valid(): I recently did something similiar, I found my code and dpasted it. In my case I am showing the view for a user to edit her existing profile. I hope it helps: http://dpaste.com/96150/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: More User/Profile troubles
I apologise for spamming everyone, here i stuck it up at dpaste with the comments which makes it a bit clearer- http://dpaste.com/96128/ On 4 Dec, 17:01, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > I dont know why the margins are squashing everything, ill try and post > my code again this time without the comments > > if request.method == 'POST': > form = UserCreationForm(request.POST) > extraform = CustomProfileForm(request.POST) > if form.is_valid(): > userformInstance = form.save() > extraformedit = extraform.save(commit=False) > extraformedit.user = userformInstance > extraformedit.save() > return HttpResponseRedirect("/") > > Hope that works a bit better. > > On 4 Dec, 16:57, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > wrote: > > > Thanks for your help Brian, Im still quite confused. I think what Im > > really not sure about is how to set the foreignkey manually as it > > doesnt really talk about that in the documentation. > > > Here is what I have now, if extraformedit.user is a foreignkey, how do > > i connect it to the form I just saved?- > > > if request.method == 'POST': > > form = UserCreationForm(request.POST) > > extraform = CustomProfileForm(request.POST) > > if form.is_valid(): > > userformInstance = form.save > > () #save the form with the user data > > and create an instance i can hook my profile to > > extraformedit = extraform.save(commit=False) > > #save the profile form with commit=false so i can modify it > > extraformedit.user = userformInstance > > #!!i think this is where im going wrong? assigning the user the > > profile is attached to > > extraformedit.save > > () #save the edited > > form to the database. > > return HttpResponseRedirect("/") > > > Am I way off? Again I am very new to this so sorry if this is a stupid > > question. > > > Phil > > > On 3 Dec, 18:52, Brian Neal <[EMAIL PROTECTED]> wrote: > > > > On Dec 3, 12:22 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > > > wrote: > > > > > ... > > > > And here is my view for my signup page - > > > > > def signup(request): > > > > if request.method == 'POST': > > > > form = UserCreationForm(request.POST) > > > > extraform = CustomProfileForm(request.POST) > > > > if form.is_valid(): > > > > form.save() > > > > extraform.save() > > > > return HttpResponseRedirect("/") > > > > else: > > > > form = UserCreationForm() > > > > extraform = CustomProfileForm() > > > > > return render_to_response('signup.html', { > > > > 'form': form, > > > > 'extraform': extraform > > > > }) > > > > > This view doesnt work because it is not saving 'user' in > > > > CustomProfileForm. > > > > > Ok now the problem I am having is with the foreignkey in the custom > > > > profile model, and successfully linking that to the User. > > > > Yes. You need to set the user foreign key inside the extraform before > > > you save it. See the discussion here about ModelForms save() with > > > commit=False: > > > >http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-sav... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: More User/Profile troubles
I dont know why the margins are squashing everything, ill try and post my code again this time without the comments if request.method == 'POST': form = UserCreationForm(request.POST) extraform = CustomProfileForm(request.POST) if form.is_valid(): userformInstance = form.save() extraformedit = extraform.save(commit=False) extraformedit.user = userformInstance extraformedit.save() return HttpResponseRedirect("/") Hope that works a bit better. On 4 Dec, 16:57, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Thanks for your help Brian, Im still quite confused. I think what Im > really not sure about is how to set the foreignkey manually as it > doesnt really talk about that in the documentation. > > Here is what I have now, if extraformedit.user is a foreignkey, how do > i connect it to the form I just saved?- > > if request.method == 'POST': > form = UserCreationForm(request.POST) > extraform = CustomProfileForm(request.POST) > if form.is_valid(): > userformInstance = form.save > () #save the form with the user data > and create an instance i can hook my profile to > extraformedit = extraform.save(commit=False) > #save the profile form with commit=false so i can modify it > extraformedit.user = userformInstance > #!!i think this is where im going wrong? assigning the user the > profile is attached to > extraformedit.save > () #save the edited > form to the database. > return HttpResponseRedirect("/") > > Am I way off? Again I am very new to this so sorry if this is a stupid > question. > > Phil > > On 3 Dec, 18:52, Brian Neal <[EMAIL PROTECTED]> wrote: > > > On Dec 3, 12:22 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > > wrote: > > > > ... > > > And here is my view for my signup page - > > > > def signup(request): > > > if request.method == 'POST': > > > form = UserCreationForm(request.POST) > > > extraform = CustomProfileForm(request.POST) > > > if form.is_valid(): > > > form.save() > > > extraform.save() > > > return HttpResponseRedirect("/") > > > else: > > > form = UserCreationForm() > > > extraform = CustomProfileForm() > > > > return render_to_response('signup.html', { > > > 'form': form, > > > 'extraform': extraform > > > }) > > > > This view doesnt work because it is not saving 'user' in > > > CustomProfileForm. > > > > Ok now the problem I am having is with the foreignkey in the custom > > > profile model, and successfully linking that to the User. > > > Yes. You need to set the user foreign key inside the extraform before > > you save it. See the discussion here about ModelForms save() with > > commit=False: > > >http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-sav... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: More User/Profile troubles
Thanks for your help Brian, Im still quite confused. I think what Im really not sure about is how to set the foreignkey manually as it doesnt really talk about that in the documentation. Here is what I have now, if extraformedit.user is a foreignkey, how do i connect it to the form I just saved?- if request.method == 'POST': form = UserCreationForm(request.POST) extraform = CustomProfileForm(request.POST) if form.is_valid(): userformInstance = form.save ()#save the form with the user data and create an instance i can hook my profile to extraformedit = extraform.save(commit=False) #save the profile form with commit=false so i can modify it extraformedit.user = userformInstance #!!i think this is where im going wrong? assigning the user the profile is attached to extraformedit.save () #save the edited form to the database. return HttpResponseRedirect("/") Am I way off? Again I am very new to this so sorry if this is a stupid question. Phil On 3 Dec, 18:52, Brian Neal <[EMAIL PROTECTED]> wrote: > On Dec 3, 12:22 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > wrote: > > > > > ... > > And here is my view for my signup page - > > > def signup(request): > > if request.method == 'POST': > > form = UserCreationForm(request.POST) > > extraform = CustomProfileForm(request.POST) > > if form.is_valid(): > > form.save() > > extraform.save() > > return HttpResponseRedirect("/") > > else: > > form = UserCreationForm() > > extraform = CustomProfileForm() > > > return render_to_response('signup.html', { > > 'form': form, > > 'extraform': extraform > > }) > > > This view doesnt work because it is not saving 'user' in > > CustomProfileForm. > > > Ok now the problem I am having is with the foreignkey in the custom > > profile model, and successfully linking that to the User. > > Yes. You need to set the user foreign key inside the extraform before > you save it. See the discussion here about ModelForms save() with > commit=False: > > http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-sav... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: More User/Profile troubles
On Dec 3, 12:22 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > ... > And here is my view for my signup page - > > def signup(request): > if request.method == 'POST': > form = UserCreationForm(request.POST) > extraform = CustomProfileForm(request.POST) > if form.is_valid(): > form.save() > extraform.save() > return HttpResponseRedirect("/") > else: > form = UserCreationForm() > extraform = CustomProfileForm() > > return render_to_response('signup.html', { > 'form': form, > 'extraform': extraform > }) > > This view doesnt work because it is not saving 'user' in > CustomProfileForm. > > Ok now the problem I am having is with the foreignkey in the custom > profile model, and successfully linking that to the User. Yes. You need to set the user foreign key inside the extraform before you save it. See the discussion here about ModelForms save() with commit=False: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
More User/Profile troubles
Hi all, I have another newbie question. The user registration + profiles seems to always be giving me a lot of trouble, I think Im nearly there but Im sort of stuck at the moment and not sure how to proceed. I am having trouble setting a user profile that is hooked up to the user all in one page. Ive set up a custom profile for my users, here is the model with a form- class CustomProfile(models.Model): user = models.ForeignKey(User, unique=True) GENDER_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) gender = models.CharField(max_length=1, choices=GENDER_CHOICES) birthday = models.DateField() country = models.CharField(max_length=50) class CustomProfileForm(ModelForm): class Meta: model = CustomProfile fields = ('gender', 'birthday', 'country') And here is my view for my signup page - def signup(request): if request.method == 'POST': form = UserCreationForm(request.POST) extraform = CustomProfileForm(request.POST) if form.is_valid(): form.save() extraform.save() return HttpResponseRedirect("/") else: form = UserCreationForm() extraform = CustomProfileForm() return render_to_response('signup.html', { 'form': form, 'extraform': extraform }) This view doesnt work because it is not saving 'user' in CustomProfileForm. Ok now the problem I am having is with the foreignkey in the custom profile model, and successfully linking that to the User. I imagine I could do it easily with two web pages - I would make a usercreationform on one page and then direct to a page to customise a profile, but I am trying to do it all at once. I have to save 'form' first, to be able to save 'extraform', but then I dont know how to set the user setting in extraform to the one ive just created. All my configurations seem to fail. I hope I havent made this too confusing, am I on the right track at least? Any help Would be great! Phil --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---