ATTENTION ATTENTION ATTENTION! I fixed it!
Brownie points to Raj for pointing to the right direction, so boys and girls, when you're trying to pass a PK (such as user.id for a OneToOne field exntending the User model) via a form, make sure you do it as follows: <input type="hidden" name="user" value="{{ user.id }}"/> and NOT something like: <input type="hidden" name="user_id" value="{{ user.id }}"/> ... because it won't work, and you'll get all sorts of crazy errors. Trust me, I've been there! Thanks all, and once again, the Django community rocks! -- Carlos Yoder http://blog.argentinaslovenia.com/ > Here you go Gábor, it's three views. my_data(request) find out what to > do (change or add), and calls the proper view, either userprofile_add > or userprofile_change. > > Hope the python code is not too ugly! > > Thanks a million for taking the time to help, köszönöm szépen! > > > def my_data(request): > """ allow a logged user to change his data. proxy view that > calls proper views (add or change) """ > > if request.user.is_anonymous(): > return redirect_to_login(request.path) > > u = User.objects.get(pk=request.user.id) > try: > up = u.userprofile # trigger exception, if not found > return userprofile_change(request, u.id) > except UserProfile.DoesNotExist: > return userprofile_add(request) > > def userprofile_add(request): > """ Handle adding new userprofiles (one2one rel to User objects) """ > > from django import forms > > manipulator = UserProfile.AddManipulator() > > if request.POST: > # If data was POSTed, we're trying to create a new > UserProfile. > new_data = request.POST.copy() > user_id = request.user.id > > # Check for errors. > errors = manipulator.get_validation_errors(new_data) > > if not errors: > # No errors. This means we can save the data! > manipulator.do_html2python(new_data) > if new_data['user_id']: > pass > else: > new_data['user_id'] = user_id > new_userprofile = manipulator.save(new_data) > > # Redirect to the object's "edit" page. Always > use a redirect > # after POST data, so that reloads don't > accidently create > # duplicate entires, and so users don't see > the confusing > # "Repost POST data?" alert box in their browsers. > return HttpResponseRedirect("/my_data/") > else: > # No POST, so we want a brand new form without any > data or errors. > errors = new_data = {} > > # Create the FormWrapper, template, context, response. > form = forms.FormWrapper(manipulator, new_data, errors) > > rc = RequestContext(request, { > 'form': form, > }) > return render_to_response('userprofile.html', rc) > > > def userprofile_change(request, userprofile_id): > """ Handle editing userprofiles (one2one rel to User objects) """ > from django import forms > > from django.http import Http404 > > try: > manipulator = UserProfile.ChangeManipulator(userprofile_id) > except UserProfile.DoesNotExist: > raise Http404 > > # Grab the object in question for future use. > userprofile = manipulator.original_object > > if request.POST: > new_data = request.POST.copy() > errors = manipulator.get_validation_errors(new_data) > if not errors: > manipulator.do_html2python(new_data) > manipulator.save(new_data) > # Do a post-after-redirect so that reload works, etc. > return HttpResponseRedirect("/my_data/") > else: > errors = {} > # This makes sure the form accurately represents the > fields of the object. > new_data = manipulator.flatten_data() > > form = forms.FormWrapper(manipulator, new_data, errors) > > rc = RequestContext(request, { > 'form': form, > 'userprofile': userprofile, > }) > > return render_to_response('userprofile.html', rc) > > Best regards, > > -- > Carlos Yoder > http://blog.argentinaslovenia.com/ > > > > Carlos Yoder wrote: > > > I'm really sorry to bug you like this, but I don't know what to do -- > > > being a newbie to both Python and Django, debugging for me is more > > > like 'aha, the problem should be around here', but nothing concrete > > > about fixing! > > > > > > > hi, > > > > could you also post your view code? > > > > gabor > --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---