Python ndb数据存储将列表放入数据中

2024-09-30 01:29:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用我以前的课程笔记填充一个数据存储区,以便将来我可以添加注释板或留言簿。在

我似乎无法在数据存储中获得之前的注释,并且一直在浏览GAE文档和搜索web,但都没有结果。在

这是我的代码,如果有人能为我指出正确的方向,我将是最伟大的。在

import time
import cgi
import os
import jinja2
import webapp2
from google.appengine.ext import ndb

jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)

dataEntery = [[1, 4, 'Networks','''
            <p>A network is a group of entities that can communicate, even </p>'''],
[2, 4, 'Measuring Networks','''
            <p>The two main ways of measuring a network are;</p>
      '''], etc]

class CourseData(ndb.Model):
    id_index = ndb.IntegerProperty()
    stage_number = ndb.IntegerProperty()
    note_title = ndb.StringProperty(indexed=False)
    note_content = ndb.StringProperty(indexed=False)

for a in dataEntery:
    newEntry = CourseData(id_index=a[0], stage_number=a[1], note_title=a[2], note_content=a[3])
    newEntry.put()
    time.sleep(.2)

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write('''
            <!DOCTYPE HTML>
                <html>
                    <head>
                        <title>Lee's Notes</title>
                        <link href="../styles.css" rel="stylesheet" type="text/css">
                    </head>
                    <body>
                    <h1 class="opener">Lee's Notes on Intro to Programming</h1>
                    ''')
        query = CourseData.query(stage_number == 4).fetch()
        self.response.out.write('<article id="note%s"><h2>%s</h2>' % cgi.escape(query.note_title), cgi.escape(query.note_title)) 
        self.response.out.write('%s</article>' % cgi.escape(query.note_content)) 

app = webapp2.WSGIApplication([
        ('/', MainHandler)], debug=True)

提前谢谢。请不要因为你比我聪明而认为我的问题很愚蠢而把我的分数记下来。我正在努力。如果你不想回答,那就继续吧。在


Tags: importselfidjinja2numbertitlecontentquery
1条回答
网友
1楼 · 发布于 2024-09-30 01:29:21

首先要注意的是appengine数据存储最终是一致的。阅读本文:https://cloud.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency?hl=en

对于你来说,你不需要查询。创建一个好的索引,然后使用钥匙。得到(). 注意,这是假设您不想使用id_索引。。。在

for a in dataEntery:
    entityKey = ndb.Key(CourseData._get_kind(), stage_number=a[1])
    newEntry = CourseData(key=entityKey, id_index=a[0], stage_number=a[1], note_title=a[2], note_content=a[3])
    newEntry.put()

然后检索变成:

^{pr2}$

相关问题 更多 >

    热门问题