title: Django, change username form
body: <p>I'm trying to create a modelform to allow users to change their username. I'm trying to do this using FormView instead of UpdateView because I want to include other forms (user management functionality) in this view eventually.</p>
  
  <p>Relevant forms.py:</p>
  
  <pre><code>    class FormHorizontalModelForm(forms.ModelForm)
          def __init__(self, *args, **kwargs):
              super(FormHorizontalModelForm, self).__init__(*args, **kwargs)
              self.helper = FormHelper()
              self.helper.form_class = 'form-horizontal'
  
      class PlayerRenameForm(FormHorizontalModelForm):
          def __init__(self, user, *args, **kwargs):
              super(PlayerRenameForm, self).__init__(*args, **kwargs)    
              self.helper.add_input(Submit('change_username', 'Change Username'))
  
          class Meta:
              model = User
              fields = ('username',)
  
          def save(self):
              pass # I figured this might be saving the object since this is inherited off ModelForm
  
          def form_valid(self, form):
              self.change_username(new_username = self.cleaned_data['username'])
              # self.send_email(new_username = self.cleaned_data['username'])
  
          def change_username(self, new_username):
              player = Player.objects.get(user = self.instance)
              self.instance.username = new_username
              self.instance.save()
              player.changed_username = True
              player.save()
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>    class AccountView(UserAuthenticationMixin, FormView):
          template_name = 'game/profile.html'
          success_url = '/accounts/'
          form_class = PlayerRenameForm
          form_class_two = CrispyPasswordSetForm
          form_class_three = CrispyPasswordChangeForm
  
          def get_context_data(self, **kwargs):
              context = super(AccountView, self).get_context_data(**kwargs)
              if 'form' not in context:
                  context['form'] = self.form_class(user = self.request.user, data = (self.request.POST or None))
              if 'password_set_form' not in context:
                  context['password_set_form'] = self.form_class_two(user = self.request.user, data = (self.request.POST or None))
              if 'password_change_form' not in context:
                  context['password_change_form'] = self.form_class_three(user = self.request.user, data = (self.request.POST or None))
              return context
  
          def form_invalid(self, **kwargs):
              return self.render_to_response(self.get_context_data(**kwargs))
  
          def get_form(self, form_class):
              return form_class(user = self.request.user, **self.get_form_kwargs())
  
          def post(self, request, *args, **kwargs):
              if 'change_username' in request.POST:
                  form = self.form_class(user = request.user, instance = request.user, data = self.request.POST)
                  form_name = 'form'
              elif 'set_password' in request.POST:
                  form = self.form_class_two(user = request.user, data = self.request.POST)
                  form_name = 'password_set_form'
              elif 'change_password' in request.POST:
                  form = self.form_class_three(user = request.user, data = self.request.POST)
                  form_name = 'password_change_form'
  
              if form.is_valid():
                  return self.form_valid(form)
              else:
                  return self.form_invalid(**{form_name: form})
  </code></pre>
  
  <p>The form returns valid, and returns to success_url. I know for a fact that it is passing into the correct part of the post function since I have stripped away the other parts to ensure that was working.</p>