Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从python字典中提取值_Python_Google App Engine_Flask - Fatal编程技术网

无法从python字典中提取值

无法从python字典中提取值,python,google-app-engine,flask,Python,Google App Engine,Flask,我正在使用谷歌Firestore。这是我的自定义对象 class Post(object): def __init__(self, Author, Message, Date): self.Author = Author self.Message = Message self.Date = Date @staticmethod def from_dict(source): # [START_EXCLUDE]

我正在使用谷歌Firestore。这是我的自定义对象

class Post(object):
    def __init__(self, Author, Message, Date):
        self.Author = Author
        self.Message = Message
        self.Date = Date

    @staticmethod
    def from_dict(source):
        # [START_EXCLUDE]
        post = Post(source[u'Author'], source[u'Message'], source[u'Date'])

        if u'Author' in source:
            post.Author = source[u'Author']
        if u'Message' in source:
            post.Message = source[u'Message']
        if u'Date' in source:
            post.Date = source[u'Date']

        return post
        # [END_EXCLUDE]

    def to_dict(self):
        # [START_EXCLUDE]
        dest = {
            u'Author':self.Author,
            u'Message':self.Message,
            u'Date':self.Date
        }

        if self.Author:
            dest[u'Author'] = self.Author

        if self.Message:
            dest[u'Message'] = self.Message

        if self.Date:
            dest[u'Date'] = self.Date

        return dest
        #[END_EXCLUDE]

    def __repr__(self):
        return(
            u'Post(Author={}, Message={}, Date={})'
            .format(self.Author, self.Message, self.Date))
我的main.py烧瓶代码的一部分

from flask import Flask
from google.cloud import firestore
from models.posts.post import Post

app = Flask(__name__)
db = firestore.Client()

@app.route('/')
def hello():
    posts_ref = db.collection(u'Posts')
    doc_ref = posts_ref.get()
    posts = Post.from_dict(doc_ref)
    retval = "Empty"
    for post in list(posts):
        retval = """Post ID : {}\n
                    Author  : {}\n
                    Message : {}""".format(post.Author, 
                                           post.Date, 
                                           post.Message)
当我运行Flask应用程序时,出现以下错误

[2019-02-23 09:20:42 +0000] [502] [INFO] Listening at: http://0.0.0.0:24197 (502)
[2019-02-23 09:20:42 +0000] [502] [INFO] Using worker: sync
  File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/vinay/site-web/main.py", line 12, in hello
    posts = Post.from_dict(doc_ref)
  File "/home/vinay/site-web/models/posts/post.py", line 10, in from_dict
    post = Post(source[u'Author'], source[u'Message'], source[u'Date'])
TypeError: 'generator' object is not subscriptable
INFO     2019-02-23 09:20:49,854 module.py:861] default: "GET /?authuser=0 HTTP/1.1" 500 291

错误很明显,您的
doc\u ref
是一个生成器。从firestore或api ref,在将此对象传递给类之前,您可能会错过
到\u dict()
。请尝试更改此
posts=Post。从\u dict(doc\u ref)
posts=Post。从\u dict(doc\u ref.到\u dict())
文件“/home/vinay/site web/main.py”,第12行,在hello posts=Post.From\u dict(doc\u ref.到\u dict())中AttributeError:'generator'对象没有属性'to#dict',然后尝试在其上迭代,看看有什么';它在这个发电机里。您自己是如何解决问题的?错误很明显,您的
doc\u ref
是一个生成器。从firestore或api ref,在将此对象传递给类之前,您可能会错过
到\u dict()
。请尝试更改此
posts=Post。从\u dict(doc\u ref)
posts=Post。从\u dict(doc\u ref.到\u dict())
文件“/home/vinay/site web/main.py”,第12行,在hello posts=Post.From\u dict(doc\u ref.到\u dict())中AttributeError:'generator'对象没有属性'to#dict',然后尝试在其上迭代,看看有什么';它在这个发电机里。你自己是怎么解决问题的?