Search code examples
jsongoogle-app-enginecompiler-errorspaas

Google App Engine JSON Property Error


I am just a beginner in python/json. I have an html doc that basically allows people to enter (1) class name and (1) prereq. In my post method, I attempted to create a new dictionary, add the (key) class name and (value) prereq. However I am getting this trace back error:

Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-  .5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2- 2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\Desktop\classes\main.py", line 27, in post
self.response.write(template.render(extracted_output))
File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2-2.6\jinja2\environment.py", line 889, in render
vars = dict(*args, **kwargs)
ValueError: dictionary update sequence element #0 has length 1; 2 is   required

Another question is when I create a new Class_Name object, is the class_title automatically a JsonProperty?

class Class_Name(ndb.Model):
    class_title = ndb.JsonProperty()

def post(self):
    classname = self.request.get('classname')
    prereq = self.request.get('prereq')
    new_dictionary = {}
    new_dictionary [classname] = prereq
    new_class = Class_Name(class_title = new_dictionary )
    new_class.put()
    dictionary_extracted = new_class.class_title
    extracted_output = json.dumps(dictionary_extracted)
    template = jinja2_environment.get_template('template/post.html')
    self.response.write(template.render(extracted_output))

Solution

  • In your code snippet:

    extracted_output = json.dumps(dictionary_extracted)
    template = jinja2_environment.get_template('template/post.html')
    self.response.write(template.render(extracted_output))
    

    json.dumps returns a string -- and then you're passing just that string to template.render... that can never work!

    If, as you say in a comment (you should edit your question to make it clearer!), what your template contains is {{extracted_output}}, then that variable name must be passed to render, i.e, the last statement must become

    self.response.write(template.render(extracted_output=extracted_output))