Saturday, September 11, 2010

Re: Integrating User Profile with Auth

I think that my main problem was that I was expecting the save to the model form to actually create the profile.  

Here's what I ended up with in my view. 

 86 @login_required
 87 def createProfile(request):
 88     UserProfile.objects.get_or_create(user=request.user)[0]
 89     if request.method == 'POST':
 90         form = UserProfileForm(request.POST, instance=request.user.get_profile())
 91         if form.is_valid():
 92             form.save()
 93             return HttpResponseRedirect("/")
 94         else:
 95             return render_to_response('fav/createProfile.tpl', { 'form' : form  }, RequestContext(request) )
 96     else:
 97         form = UserProfileForm(instance=request.user.get_profile())
 98     return render_to_response('fav/createProfile.tpl', { 'form' : form }, RequestContext(request))
 99


On Sat, Sep 11, 2010 at 12:37 AM, Shawn Milochik <shawn@milochik.com> wrote:
I think you just may be missing a call to get_profile() in this view.
You can just do that and do a try block with an except block for
DoesNotExist. That will let you know the situation you're in, whether
the profile already existed or not.

Also, unless I'm misreading something you're trying to pass a User
instance as the instance for a user profile, which will not work. If
anything, you should be doing a .get() on the user profile where user
= request.user, but that doesn't matter anyway because get_profile()
does the same thing and is the correct way to do this.

Shawn

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment