TypeError: __init__() got an unexpected keyword argument

19,162

Solution 1

I'm assuming you're using Django Templates. From: https://developers.google.com/appengine/docs/python/gettingstarted/templates

template.render(path, template_values) takes a file path to the template file and a dictionary of values, and returns the rendered text.

Note that render takes only two arguments: a path to a template, which you provided, and a single dictionary that has all the other values the template expects.

You gave it all those values as arguments, rather than packed into a dictionary.

Change:

self.render('newcase.html', title=title, facts=facts, holding=holding, ... )

to:

self.render('newcase.html', {title=title, facts=facts, holding=holding, ...})

and similarly for any other render calls.

Solution 2

After running into the same problem, I found that my problem was in the webapp2.WSGIApplication Route definitions.

I had defined my Route as follows e.g.

webapp2.Route('/api/something/anEndpoint', handlers.SomethingEndpointHandler)

However, that route wasn't even reached because another Route that captured a regex keyword, defined earlier in the Route list, was being triggered instead:

webapp2.Route('/api/something/<something_id>', handlers.SomethingElseHandler)

This caused the TypeError in OP to appear with unexpected keyword argument 'something_id'.

A very difficult error to debug but hopefully this helps someone!

Share:
19,162
chlamb16
Author by

chlamb16

Updated on June 14, 2022

Comments

  • chlamb16
    chlamb16 almost 2 years

    Below is the code that I use for the form to enter new cases into the GAE datastore. When I try to enter the form I get the type error below saying I am using an unexpected keyword argument. I am new to python and GAE does anyone have any idea what I'm doing wrong?

    class Case(db.Model):
        user = db.StringProperty(required = True)
        title = db.StringProperty(required = True)
        facts = db.TextProperty()
        holding = db.TextProperty()
        rule_of_law = db.TextProperty()
        general_notes = db.TextProperty()
        key_concepts = db.TextProperty()        
    
    class NewCase(Handler):
        def get(self,title="",
                facts="",
                holding="",
                rule_of_law="",
                general_notes="",
                key_concepts="",
                error=""):
    
            if self.user:
                self.render('newcase.html', title=title,
                            facts=facts,
                            holding=holding,
                            rule_of_law=rule_of_law,
                            general_notes=general_notes,
                            key_concepts=key_concepts,
                            error=error)
            else:
                self.redirect('/login')
    
    def post(self):
        if not self.user:
            self.redirect('/')
    
        user = self.read_secure_cookie('user_id')    
        self.title = self.request.get('title')
        self.facts = self.request.get('facts')
        self.holding = self.request.get('holding')
        self.rule_of_law = self.request.get('rule_of_law')
        self.general_notes = self.request.get('general_notes')
        self.key_concepts = self.request.get('key_concepts')
    
        if self.title:
            c = Case(user = user,
                     title = self.title,
                     facts = self.facts,
                     holding = self.holding,
                     rule_of_law = self.rule_of_law,
                     general_notes = self.general_notes,
                     key_concepts = self.key_concepts)
    
            c.put()
    
            self.redirect('/%s' % c.key().id())
    
        else:
            error = "You must enter a title."
            self.render('newcase.html',title = self.title,
    

    Error I'm getting:

    File "C:\Users\Chris\Documents\Web Apps\legalstudybuddy\main.py", line 233, in post
        key_concepts = self.key_concepts)
    TypeError: __init__() got an unexpected keyword argument 'rule_of_law'
    
  • chlamb16
    chlamb16 over 11 years
    The problem doesn't appear to be with the render call as I use other render calls with multiple variables and it works just fine. I think the type error is referring to an unexpected keyword argument while creating the GAE entity Case. Edit Line 233 is the line in c = Case(...)