def user_info(request, template_name='social/retrieve_user_data.html', username=None):
user = User.objects.get(username=username)
if request.method == 'POST':
form = UserInfoForm(request.POST, user)
print(form.is_valid())
if form.is_valid():
form.save()
else:
print('not post')
form = UserInfoForm(user)
return render_to_response(template_name, RequestContext(request, {
'form': form,
}))
class UserInfoForm(forms.Form):
def __init__(self,*args,**kwargs):
self.user = kwargs.pop('user')
super(UserInfoForm,self).__init__(*args,**kwargs)
This is producing a KeyError with exception value u'user'. What is wrong here? In both cases, form is initialized with a valid value of user. Why am I getting a keyerror>
You're not passing the user as a keyword argument in either the if or the else block. It should be:
form = UserInfoForm(request.POST, user=user)
and:
form = UserInfoForm(user=user)
Only func(foo=bar)can put {"foo":bar} into kwargs dictionary! According to your code form = UserInfoForm(request.POST, user),you can use self.user = args[1] to capture user.