Monday, August 30, 2010

Django UserProfile's - the hardest part of the whole framework

So, I'm still newer to django. I have user registration,
authentication, imagefields, filters, and more all built into my
application and working. So now, I need a user to be able to edit his
own email and upload an image to the user_profile. First, I am using
RegistrationProfile for the django registration just fine, should be
no conflict right?

Here's my problem, I know you don't get direct access to the user
model, so making the user email editable is kind of tricky here's my
FAILED attempt:

from settings.py:

AUTH_PROFILE_MODULE = "gather.models.UserProfile"

From models.py:

class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
image = models.ImageField(null=True,upload_to='images/users')

def save(self, *args, **kwargs):
super(UserProfile, self).save(*args, **kwargs)

def update(self, *args, **kwargs):
super(UserProfile, self).update(*args, **kwargs)

def delete(self, *args, **kwargs):
super(UserProfile, self).delete(*args, **kwargs)

class UserProfileForm(UserProfileForm):
class Meta:
model = UserProfile
exclude = ['user']


from forms.py:

class UserProfileForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(UserProfileForm, self).__init__(*args, **kwargs)
try:
self.fields['email'].initial = self.instance.user.email
# self.fields['first_name'].initial =
self.instance.user.first_name
# self.fields['last_name'].initial =
self.instance.user.last_name
except User.DoesNotExist:
pass
email = forms.EmailField(label="Primary email",help_text='')
image = forms.ImageField(required=False)

def save(self, *args, **kwargs):
"""
Update the primary email address on the related User object as
well.
"""
u = self.instance.user
u.email = self.cleaned_data['email']
u.save()
profile = super(UserProfileForm, self).save(*args,**kwargs)
return profile

from views.py:

def account(request):
#return render_to_response('account.html',
context_instance=RequestContext(request))
if request.method == "POST":
form =
UserProfileForm(request.user,request.POST,request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/account/')
else:
form = UserProfileForm(request.user)
return render_to_response('account.html', {'form': form},
context_instance=RequestContext(request))


So, this seems simple enough but doesn't work, here's my output:

Caught AttributeError while rendering: 'User' object has no attribute
'get'

Thanks, as I am at a stoppage.
appreciated.

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