Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

513
Views
KeyError in Django form kwargs
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>

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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)
about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!