How to ajaxable validate a PasswordChangeForm?
<p>I have some forms that I want to validate with an ajax view.</p>

<pre><code>class ProfileEditPasswordForm(PasswordChangeForm):
    class Meta:
        model = User

class AjaxValidation(generic.edit.FormView):
    def get(self, request, *args, **kwargs):
        return HttpResponseRedirect('/')

    def form_invalid(self, form):
        data = []
        for k, v in form._errors.iteritems():
            text = {
                'desc': ', '.join(v),
            }
            if k == '__all__':
                text['key'] = '#%s' % self.request.POST.get('form')
            else:
                text['key'] = '#id_%s' % k
            data.append(text)
        return HttpResponse(json.dumps(data))

    def form_valid(self, form):
        return HttpResponse("ok")

    def get_form_class(self):
        form_dict = {
            'signup': forms.RegisterForm,
            'signin': forms.LoginForm,
            'safety': forms.ProfileEditPasswordForm
        }
        return form_dict[self.request.POST.get('form')] #=&gt; TypeError
</code></pre>

<p>As you can see, I have 3 different forms. First two work just fine. But the third one returns me an error.</p>

<blockquote>
  <p><code>__init__() takes at least 2 arguments (1 given)</code></p>
</blockquote>

<p>I believe that it happens because my form requires a user to validate his old password, so I did this:</p>

<pre><code>'safety': forms.ProfileEditPasswordForm(user=self.request.user)
</code></pre>

<p>And know it throws another error:</p>

<blockquote>
  <p><code>'ProfileEditPasswordForm' object is not callable</code></p>
</blockquote>

<p>And here I stuck.</p>

<hr>

<pre><code>Traceback:
File "***/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "***/env/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "***/env/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  89.         return handler(request, *args, **kwargs)
File "***/env/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
  213.         form = self.get_form()
File "***/env/local/lib/python2.7/site-packages/django/views/generic/edit.py" in get_form
  74.         return form_class(**self.get_form_kwargs())

Exception Type: TypeError at /accounts/validate/
Exception Value: 'ProfileEditPasswordForm' object is not callable
Request information:
GET: No GET data

POST:
csrfmiddlewaretoken = u'MjSRpY4NQsTuv6hG8HLYZYI3n9HpPU4R'
form = u'safety'
old_password = u''
new_password1 = u''
new_password2 = u''
</code></pre>

<p>I think the problem is that django <code>FormView</code> is not compatible with <code>PasswordChangeForm</code>. The form requires the user to be passed to its constructor, but the view will not do that. You could try overriding <code>get_form_kwargs</code> method like this:</p>

<pre><code>class AjaxValidation(generic.edit.FormView):
    # your methods
    def get_form_kwargs(self):
        kwargs = super(AjaxValidation, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs
</code></pre>

<p>The <code>kwargs</code> will be passed to form constructor and it should work. <code>get_form_class</code> is OK in your main code example</p>

python ajax django python-2.7 django-1.8