updating a user form?

17 views
Skip to first unread message

raj

unread,
Jun 22, 2011, 2:28:16 AM6/22/11
to Django users
I'm trying to create an update_profile form. This form will be able to
update extended user information that is not in the original form.

So, I have a form that allows you to create an account. There a lot of
other fields that aren't listed in this form. The rest of these fields
will be found when the user actually logs in. What I can't figure out
is how to make the class that allows them to update this information.
Like in the extended user class that I made, I have a save function
that creates a user and saves it. But I don't want to create another
user with this form. I simply want to update the current authenticated
user. I thought there would be an update_user type function in the
UserManager(), but there isn't. I tried googling and didn't come up
with much. Help Please?

Herman Schistad

unread,
Jun 22, 2011, 2:37:38 AM6/22/11
to django...@googlegroups.com

I don't quite understand what you mean...

When you say load the url: /userprofile/1234/ at got your urls.py to
get <id> from the URL.
Then you could load that userprofile from the db with e.g.

def a_nice_view(request, id):
from django.auth.models import User
userprofile = User.objects.get(pk=id)
## Then do updating on that profile here ##
userprofile.username = "Somethingnew"

It's the same way with a form/request, and you can get the userprofile
with: request.user

All this is pretty basic stuff, and as I've seen you ask a lot of
these questions on django-users I would really advise you to read a
introductory book or do some more tutorials before trying to make
something advanced yourselves.

--
With regards, Herman Schistad

Kevin Renskers

unread,
Jun 22, 2011, 4:08:01 AM6/22/11
to django...@googlegroups.com
I'd advise you to read https://docs.djangoproject.com/en/1.3/topics/auth/#storing-additional-information-about-users. So, create a separate model for the profile, which can be edited by the user once he is logged in.

raj

unread,
Jun 22, 2011, 4:14:55 AM6/22/11
to Django users
This is an interesting way of doing it. I don't have it set up so that
every user has their own url that contains their db id. Django checks
to see if the user is authenticated or not. If I didn't have the url
set up in this manner, how would I manage to extract the userprofile?
I was seeing something called request.user, but I couldn't get this to
work.
I googled some more and I got a post talking about the ModelForm. This
allowed me to restrict the form fields for the user profile. The issue
is that each form field didn't have the default values set to those in
the database. (The form "value" parameter). I looked through the
ModelForm section of the djangobook, as well as the forms section, and
I couldn't come up with anything.

On Jun 22, 2:37 am, Herman Schistad <herman.schis...@gmail.com> wrote:

Herman Schistad

unread,
Jun 22, 2011, 5:05:42 AM6/22/11
to django...@googlegroups.com
On Wed, Jun 22, 2011 at 10:14, raj <nano....@gmail.com> wrote:
> If I didn't have the url
> set up in this manner, how would I manage to extract the userprofile?

If you have the "AuthenticationMiddleware" activated you would use request.user
This returns a User object, which you then can use to further edit things.

E.g.
user = request.user
user.username = "Somethingnew"
user.save()

Or, as Kevin Renskers said, you would create a seperate model for the
userprofile if you wanted to extend it. Here you will need to get the
user profile with user.get_profile() and remember to set the
following: AUTH_PROFILE_MODULE = "myapp.mysiteprofile"

Then you could use the above example like this:
user = request.user
user.get_profile().homepage = "http://example.org"
user.save()

raj

unread,
Jun 22, 2011, 5:11:33 PM6/22/11
to Django users
I think I'm starting to get the hang of it a little. I was able to
extract the data from the profile and set it as the initial value.
From what I understand, you would like me to set each user field one
at a time, but if I have 20-30 fields that need to be updated at once,
I'm at a loss. The way that I have my files set up is that I have a
view with the following code:
### views.py ###
def profileFields(request):
user = request.user
if request.method == 'POST':
form = userProf(request.POST)
if form.is_valid():
user = form.save(request.POST, user)
return HttpResponseRedirect('/user/update/success')
else:
form = userProf(instance=user.get_profile())
return render_to_response("/home/oneadmin/webapps/oneadmin/
oneadmin/templates/oneadmissions/profile.html", {'form': form},
context_instance = RequestContext(request))

now my userProf form module has the following save function:
def save(self, new_data, user):
user.get_profile().colleges = new_data['colleges']
.....dozen other new_data fields would go here...
user.save()
return user

From what I understand, you want me to put the save information in the
views.py file, but my actual form class is in a different document. So
i decided that it would be better to put it there. Am I on the right
track? Why isn't any information actually being updated?


On Jun 22, 5:05 am, Herman Schistad <herman.schis...@gmail.com> wrote:

raj

unread,
Jun 23, 2011, 1:03:08 AM6/23/11
to Django users
nevermind, i figured it out. thanks anyway
Reply all
Reply to author
Forward
0 new messages