def dispatch(self, request, *args, **kwargs): action = request.POST.get('action') self.profile_form = UserChangeProfileForm( (request.POST if action == 'profile' else None), (request.FILES if action == 'profile' else None), instance=request.user, prefix='profile' ) self.password_form = UserPasswordChangeForm(request.user, (request.POST if action == 'password' else None), prefix='password') self.email_form = UserEmailChangeForm(request.user, request.POST if action == 'email' else None, prefix='email') return super(UserSettingsView, self).dispatch(request, *args, **kwargs)
class UserSettingsView(TemplateView): template_name = 'users/settings.html' @method_decorator(login_required) def dispatch(self, request, *args, **kwargs): action = request.POST.get('action') self.profile_form = UserChangeProfileForm( (request.POST if action == 'profile' else None), (request.FILES if action == 'profile' else None), instance=request.user, prefix='profile' ) self.password_form = UserPasswordChangeForm(request.user, (request.POST if action == 'password' else None), prefix='password') self.email_form = UserEmailChangeForm(request.user, request.POST if action == 'email' else None, prefix='email') return super(UserSettingsView, self).dispatch(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(UserSettingsView, self).get_context_data(**kwargs) context['profile_form'] = self.profile_form context['password_form'] = self.password_form context['email_form'] = self.email_form return context def post(self, request, *args, **kwargs): if self.profile_form.is_valid(): self.profile_form.save() messages.success(request, _(u'Вы успешно изменили свой профиль.')) return redirect(request.path) elif self.password_form.is_valid(): self.password_form.save() request.user.backend = request.session[BACKEND_SESSION_KEY] login(request, request.user) messages.success(request, _(u'Пароль успешно изменен.')) return redirect(request.path) elif self.email_form.is_valid(): self.email_form.save() messages.success(request, _(u'Email успешно изменен.')) return redirect(request.path) return self.get(request, *args, **kwargs)