Friday, September 10, 2010

Integrating User Profile with Auth

I am following the information in the docs to add fields to maintain, in addition to the ones Auth already maintains.  I'm starting with the docs here

So far, I have a model form I've created based on the additional fields.  And, I'm displaying my form in my template.  But, I can't seem to get the form to work.  My plan is to use the exception encountered when get_profile() is called in another view, but the profile doesn't yet exist.  I want to catch the error and send the user to a form to fill out the extra field I want to maintain and pre-populate another.

Mostly I think my problem arises when I try to save my form.  But, it could be when I create it.  The form is displaying a drop down list box of all of the users.  So, something odd is going on there.  Also, every time I submit the form, the submit method is GET, even though the method is set to POST in the form's attributes. 

I believe I may be stumbling over this part of the docs:

"The method get_profile() does not create the profile, if it does not exist. You need to register a handler for the signaldjango.db.models.signals.post_save on the User model, and, in the handler, if created=True, create the associated user profile"

A suggestion is made here to create the profile.
 User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
But, if I automatically create the profile, I can't catch the exception for a missing profile.

Here's some code:

In my template, I have this code (line 93 seems to be instrumental in my problem).  I can get a post if I use line 92 instead of line 93.  But, I still have 2 problems.  First, it's letting me choose the user (I was hoping the code on line 90 would eliminate that).  And, second, the form simply won't save.:

 86 @login_required
 87 def createProfile(request):
 88     err_msg = [request.method] # I have this so I can see the method
 89     messages = []
 90     a = User.objects.get(username=request.user.username)
 91     # err_msg = [request.method, a]
 92     # form = UserProfileForm(instance=a)
 93     form = UserProfileForm(request.POST, instance=a)
 94     if request.method == 'POST':
 95         if form.is_valid():
 96             form.save()
 97             return HttpResponseRedirect("/")
 98         else:
 99             err_msg.append("Error Processing File")
100             return render_to_response('fav/createProfile.tpl', { 'ERROR_MSG' : err_msg, 'messages' : messages, 'form' : form  }, RequestContext(request) )
101     return render_to_response('fav/createProfile.tpl', { 'ERROR_MSG' : err_msg, 'form' : form }, RequestContext(request))

My Model:

  1 from django.db import models
  2 from django.forms import ModelForm
  3 from django.contrib.auth.models import User
  4
  5 class UserProfile(models.Model):
  6     user = models.ForeignKey(User, unique=True)
  7     screenname = models.CharField('Screen Name', max_length=25, unique=True)
  8     credits = models.PositiveSmallIntegerField('Credits', default=3)
  9
 10 class UserProfileForm(ModelForm):
 11     class Meta:
 12         model = UserProfile
 13         #fields = ['screenname'] # I've tried uncommenting this.
~                                    

--
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