Python 在google app engine中更改字典的键-使用不同的方法解决
将键值更改为字典值时遇到问题Python 在google app engine中更改字典的键-使用不同的方法解决,python,google-app-engine,app-engine-ndb,Python,Google App Engine,App Engine Ndb,将键值更改为字典值时遇到问题 def get(self): #Get all the Subjects subjects = ndb.gql('SELECT name,order FROM Subject ORDER BY order ASC') values = {'subjects':subjects} #Get all the Contents for subject in subjects: contents = ndb.gql('SELECT * FRO
def get(self):
#Get all the Subjects
subjects = ndb.gql('SELECT name,order FROM Subject ORDER BY order ASC')
values = {'subjects':subjects}
#Get all the Contents
for subject in subjects:
contents = ndb.gql('SELECT * FROM Content WHERE ANCESTOR IS :1 ORDER BY order ASC',subject.key)
values[subject.name] = contents #***HERE is the issue***
而不是拿字典
value={key:value}
我正试着去
value={{key:value}:value}
提前感谢您的建议
编辑: 当我尝试
值['subject':subject.name]=内容
我得到了错误
TypeError:不可损坏的类型
已解决:采用不同的方法:
def get(self):
#Get all the Subjects
subjects = ndb.gql('SELECT name,order FROM Subject ORDER BY order ASC')
values = {'subjects':subjects}
#Get all the Contents
values['contents'] = []
for subject in subjects:
#Formatting HTML output
subjectAll = subject.name + ' ' + subject.order
contents = ndb.gql('SELECT name,order FROM Content WHERE ANCESTOR IS :1 ORDER BY order ASC',subject.key)
values['contents'].append(subjectAll)
for content in contents:
#Formatting HTML output
contentAll = content.name + ' ' + content.order
values['contents'].append(contentAll)
事实上,我刚刚找到了一个不同的解决方案。我发现在main.py中为HTML格式化数据并将其发送到HTML更容易,而不是用HTML格式化数据。像我使用替代方法那样用python脚本格式化HTML,而不是用实际的HTML,这通常是好的还是坏的做法???