Tuesday, August 31, 2010

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

On 31 août, 06:16, reduxdj <patricklemi...@gmail.com> wrote:
> 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?

Nope, these are distinct apps and needs and whatnot...

> Here's my problem, I know you don't get direct access to the user
> model,

Yes you do.

> so making the user email editable is kind of tricky

Why so ?

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

You don't need any of these three methods - your current
implementation just duplicate what would happen without them.

> class UserProfileForm(UserProfileForm):
>     class Meta:
>         model = UserProfile
>         exclude = ['user']
>
> from forms.py:
>
> class UserProfileForm(forms.ModelForm):

I personnaly don't use a ModelForm for user profiles - since you
(usually) have to mix&match data from both the user and profile
models, I find it simpler to use a plain forms.Form and handle the
fields/models mapping, saving etc by myself

(snip form's code - not sure whether it's ok)

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

You should have a full traceback, it might help if you post it so we
know where the exception is raised. But from your code and the error
message, I'd say the problem is somewhere in your template (wild
guess...).

> 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