def profile_view(request): """ Allow user to add personnal data to his profile """ profile = request.user.get_profile() context = {"current": "account"} if request.method == "POST": profile_form = ProfileForm(instance=profile, data=request.POST) user_form = UserForm(instance=request.user, data=request.POST) if profile_form.is_valid() and user_form.is_valid(): profile_form.save() user_form.save() context["profile_saved"] = True else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=profile) context.update({"user_form": user_form, "profile_form": profile_form, "current": "account"}) return render_response(request, "profile/profile.html", context)
def profile_view(request, template_name='profile_form.html'): """ Allow user to add personnal data to his profile if the profile is saved, a context variable profile_saved is set to True """ profile = request.user.get_profile() context = {} if request.method == "POST": profile_form = ProfileForm(instance=profile, data=request.POST) user_form = UserForm(instance=request.user, data=request.POST) if profile_form.is_valid() and user_form.is_valid(): profile_form.save() user_form.save() context['profile_saved'] = True else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=profile) context.update({'user_form': user_form, 'profile_form': profile_form, 'current':'account'}) return render_response(request, template_name, context)
def update_profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() return redirect('profile-home') else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'profile/profile.html', { 'user_form': user_form, 'profile_form': profile_form })