Why this code does not change users firstname and lastname?

109 views
Skip to first unread message

joulumaa

unread,
Jan 19, 2015, 4:22:02 AM1/19/15
to django...@googlegroups.com
I have created user earlier in code with email address and password. Created users work fine.
Now in later phase I asked more information and would like to fill in first_name and last_name.
Code shows old first_name and last_name properly if set in admin view, but logged in user cannot save his information for some reason?

  p=User.objects.get(username=request.user.username)
    if request.method == 'POST':
        form = AdditionalCandidateInfo(request.POST)
        if form.is_valid():
            p.first_name=form.cleaned_data['first_name']
            p.last_name=form.cleaned_data['last_name']
            p.save()
            return HttpResponseRedirect('/')
    else:
        form = AdditionalCandidateInfo(initial={'first_name':p.first_name,'last_name':p.last_name})
      

-thanks for help

Daniel Roseman

unread,
Jan 19, 2015, 4:26:47 AM1/19/15
to django...@googlegroups.com
You should be using a ModelForm, and pass the `instance` argument when instantiating the form.

Also there's no point querying User: `request.user` is *already* the relevant User instance.
--
DR.

joulumaa

unread,
Jan 19, 2015, 7:39:38 AM1/19/15
to django...@googlegroups.com
Why would it make sense to switch from different form type?
I use below form to get information that I like to save to user database?
Part of information I need ot update goes to Additional user info class, I would like to stay in my own form.

-Thanks

Jayadeep Karnati

unread,
Jan 19, 2015, 11:58:07 PM1/19/15
to django...@googlegroups.com
Looks like you really want to stick with normal forms. I see there is nothing wrong with the code you've posted. Can you also post the snippets of AdditionalCandidateInfo() and your template.

Daniel Roseman

unread,
Jan 20, 2015, 5:30:17 AM1/20/15
to django...@googlegroups.com
On Monday, 19 January 2015 12:39:38 UTC, joulumaa wrote:
Why would it make sense to switch from different form type?
I use below form to get information that I like to save to user database?
Part of information I need ot update goes to Additional user info class, I would like to stay in my own form.

-Thanks


Well you can do that with a ModelForm, but fair enough.

You haven't shown the rest of your view, or any of the form. Perhaps it is not valid? 
--
DR. 
Reply all
Reply to author
Forward
0 new messages