python - Django, change username form -


i'm trying create modelform allow users change username. i'm trying using formview instead of updateview because want include other forms (user management functionality) in view eventually.

relevant forms.py:

    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 # figured might saving object since 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() 

views.py

    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}) 

the form returns valid, , returns success_url. know fact passing correct part of post function since have stripped away other parts ensure working.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -