This line of code:
contract = Contract.get_by_id(contract_id)
From this block:
class MainHandler(webapp2.RequestHandler):
def get(self):
contract_id = self.request.get("contract_id")
if contract_id is None:
contract = db.GqlQuery("SELECT * FROM Contract ORDER BY date DESC").get()
if contract == None:
numBook = 1
numInitialPage = 1
numFinalPage = 1
else:
contract = Contract.get_by_id(contract_id)
template_values = {"numBook":numBook,
"numInitialPage":numInitialPage,
"numFinalPage":numFinalPage}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
Is producing this error:
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\Py\Desktop\contract\main.py", line 277, in get
contract_id = self.request.get("contract_id")
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1292, in get_by_id
ids, multiple = datastore.NormalizeAndTypeCheck(ids, (int, long))
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 139, in NormalizeAndTypeCheck
(types, arg, typename(arg)))
BadArgumentError: Expected an instance or iterable of (<type 'int'>, <type 'long'>); received (a str).
My goal is: if a value was passed through variable contract_id when other handler redirect to MainHandler, MainHandler should use it. But if MainHandler is being used for the first time, then contract_id value should be None and the program should to another thing. But this isn’t happening. When MainHandler is called, it interpret ‘contract_id’ (from self.request.get("contract_id")) as a string value. How to fix that and achieve my goal?
What the error is telling you is that it expected an
intor a list ofint. This is consistent with the docs. So you will need first to convertcontract_idto anint.Also you need to change how you check if the
contract_idwas passed. Theself.request.getmethod returns an empty string when the value is not defined. See http://webapp-improved.appspot.com/api/webapp2.html#webapp2.Request.get.So you have to alternatives here:
or