def post(self, request, *args, **kwargs): user = get_object_or_404(User, id=self.request.user.id) member = get_object_or_404(Member, user=user) user_edit_form = UserEditForm(data=request.POST, instance=user) password_edit_form = ChangePasswordForm(data=request.POST, user=user, initial={}) member_edit_form = MemberDetailsForm(data=request.POST, instance=member) if user_edit_form.is_valid(): user_edit_form.save() if password_edit_form.has_changed(): if password_edit_form.is_valid(): password_edit_form.save() return redirect(reverse('users:login')) if member_edit_form.is_valid(): member_edit_form.save() context = self.get_context_data(**kwargs) context['form'] = user_edit_form context['form2'] = password_edit_form context['form3'] = member_edit_form return render_to_response(self.template_name, context, context_instance=RequestContext(request))
def Register(request): """ If POST then process forms and create relevant database entries Otherwise check if the user is already logged in. If not then show registration forms. """ if request.method == 'POST': user_create_form = UserCreateForm(data=request.POST) user_details_form = MemberDetailsForm(data=request.POST) user_address_form = MemberAddressForm(data=request.POST) if (user_create_form.is_valid() and user_details_form.is_valid() and user_address_form.is_valid()): user = user_create_form.save() user.is_staff = False user.save() details = user_details_form.save(commit=False) details.user = user details.save() address = user_address_form.save(commit=False) address.resident = user address.active = True address.save() # TODO redirect somewhere more sensible? login? return redirect(reverse('users:login')) else: return render_to_response('users/register.html', { 'form': user_create_form, 'form2': user_details_form, 'form3': user_address_form, }, context_instance=RequestContext(request)) elif not request.user.is_authenticated(): user_create_form = UserCreateForm() user_details_form = MemberDetailsForm() user_address_form = MemberAddressForm() return render_to_response('users/register.html', { 'form': user_create_form, 'form2': user_details_form, 'form3': user_address_form, }, context_instance=RequestContext(request)) return redirect(reverse('users:index'))