On 2/10/07, Lawrence Oluyede <[EMAIL PROTECTED]> wrote:
>
> > I now want to take an existing user record and create an edit account
> > form, where the data entered by the user in the registration form is
> > represented and available to edit. Seemed simple when I set out to do
> > it, but I can't seem to populate the form.
>
> It's pretty easy. You provide a page with the edit link and you create
> a user_edit view or whatever.
>
> Now in that view you simply check if the user is authenticated and
> then get his id.
>
> > Should I define which data goes into the form fields using the
> > 'initial' data in the Form definition? If so, how would I do this,
> > because I don't know the user_id and can't place a -
> > userid=request.user.id request in this definition.
>
> I don't really understand why you can't but you don't need the ID
> because the authenticated user is already in request.user attribute
>
> Anyway the pattern is:
>
> def profile_edit(request):
> if not request.user.is_authenticated():
> # blah blah
>
> if request.method == "POST":
> new_data = request.POST.copy()
is there any reason why you copy the data? the form doesn't need the
data to be mutable, so there is no need for this.
>
> form = UserEditForm(new_data)
> if form.is_valid():
> clean_data = form.clean_data
> # blah blah
> return HttpResponseRedirect("/whatever/")
> else:
> data = _get_user_data(request)
> form = UserEditForm(initial=data)
>
> context = Context({'form': form})
> return render_to_response('blahblah/account.html', context)
>
> In this way when the page is accesed with a GET you call
> _get_user_data() to retrieve the data to put in the form otherwise you
> check for errors and validate.
>
> In _get_user_data() you actually populate the dictionary with
> something like this:
>
> def _get_customer_profile_data(request):
> first_name = request.user.first_name
> last_name = request.user.last_name
> # go on with the other fields
>
> # if you have a profile associated
> user_profile = request.user.get_profile()
> whatever = user_profile.whatever
> del user_profile
> return locals()
I prefer constructing a dict manually than to mess around with python
internals, but that is really just a matter of personal preference.
>
> HTH
>
> --
> Lawrence, oluyede.org - neropercaso.it
> "It is difficult to get a man to understand
> something when his salary depends on not
> understanding it" - Upton Sinclair
>
> >
>
--
Honza Kr�l
E-Mail: [EMAIL PROTECTED]
ICQ#: 107471613
Phone: +420 606 678585
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---