如何使用 python 将表单中的数据放入数据存储中 - 谷歌应用程序引擎

发布于 2024-12-04 04:17:40 字数 3548 浏览 1 评论 0原文

我是谷歌应用引擎的初学者,我有这个问题。

我尝试创建一个表单,当您提交时将所有数据放入数据存储中。例如

class Book(db.Model):
    #book_id = db.key
    title = db.StringProperty()
    author = db.StringProperty()
    #copies = db.IntegerProperty()
    category = db.StringProperty()

class GuestBook(webapp.RequestHandler): def get(self):
        self.response.out.write("""
      <html>
        <body>
          <form action="/sign" method="post">
            <div>Title: <input type="text" name="title"</div>
    <div>Author: <input type="text" name="author"</div>
    <div>Copies: <input type="text" name="copies"</div>
    <div><select>
        <option name="category" value="adventure">Adventure</option>
        <option name="category" value="comedy">Comedy</option>
        <option name="category" value="dramatic">Dramatic</option>
        <option name="category" value="mystery">Mystery</option>
        <option name="category" value="science_fiction">Science Fiction</option></select>
    <div><input type="submit" value="Add Book"></div>
          </form>
        </body>
      </html>""")

,我尝试了这个,

class Add(webapp.RequestHandler):
    def post(self):

        Book.title = self.request.get('title')
        Book.author = self.request.get('author')
        Book.category = self.request.get('category')
        book = Book()
        book.put()

但我得到了这些

    Traceback (most recent call last):
  File "/home/kostas89/google_appengine/google/appengine/ext/webapp/__init__.py", line 636, in __call__
    handler.post(*groups)
  File "/home/kostas89/library/library.py", line 59, in post
    book.put()
  File "/home/kostas89/google_appengine/google/appengine/ext/db/__init__.py", line 984, in put
    return datastore.Put(self._entity, config=config)
  File "/home/kostas89/google_appengine/google/appengine/api/datastore.py", line 455, in Put
    return _GetConnection().async_put(config, entities, extra_hook).get_result()
  File "/home/kostas89/google_appengine/google/appengine/datastore/datastore_rpc.py", line 629, in get_result
    self.check_success()
  File "/home/kostas89/google_appengine/google/appengine/datastore/datastore_rpc.py", line 599, in check_success
    rpc.check_success()
  File "/home/kostas89/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 558, in check_success
    self.__rpc.CheckSuccess()
  File "/home/kostas89/google_appengine/google/appengine/api/apiproxy_rpc.py", line 156, in _WaitImpl
    self.request, self.response)
  File "/home/kostas89/google_appengine/google/appengine/api/datastore_file_stub.py", line 710, in MakeSyncCall
    response)
  File "/home/kostas89/google_appengine/google/appengine/api/apiproxy_stub.py", line 87, in MakeSyncCall
    method(request, response)
  File "/home/kostas89/google_appengine/google/appengine/api/datastore_file_stub.py", line 793, in _Dynamic_Put
    self.__WriteDatastore()
  File "/home/kostas89/google_appengine/google/appengine/api/datastore_file_stub.py", line 643, in __WriteDatastore
    self.__WritePickled(encoded, self.__datastore_file)
  File "/home/kostas89/google_appengine/google/appengine/api/datastore_file_stub.py", line 699, in __WritePickled
    os.rename(tmp_filename, filename)
OSError: [Errno 21] Is a directory

,所以我确信这种“方式”(我在论坛中找到的)是完全错误的。有关我如何放置数据以及如何检索它的任何建议(但这是另一个故事)? 谢谢指教。

I am a beginner in Google app engine and i have this question.

I try to create a form and when you submit to put all data in datastore. for exmble

class Book(db.Model):
    #book_id = db.key
    title = db.StringProperty()
    author = db.StringProperty()
    #copies = db.IntegerProperty()
    category = db.StringProperty()

class GuestBook(webapp.RequestHandler): def get(self):
        self.response.out.write("""
      <html>
        <body>
          <form action="/sign" method="post">
            <div>Title: <input type="text" name="title"</div>
    <div>Author: <input type="text" name="author"</div>
    <div>Copies: <input type="text" name="copies"</div>
    <div><select>
        <option name="category" value="adventure">Adventure</option>
        <option name="category" value="comedy">Comedy</option>
        <option name="category" value="dramatic">Dramatic</option>
        <option name="category" value="mystery">Mystery</option>
        <option name="category" value="science_fiction">Science Fiction</option></select>
    <div><input type="submit" value="Add Book"></div>
          </form>
        </body>
      </html>""")

and I tried this

class Add(webapp.RequestHandler):
    def post(self):

        Book.title = self.request.get('title')
        Book.author = self.request.get('author')
        Book.category = self.request.get('category')
        book = Book()
        book.put()

But i got these

    Traceback (most recent call last):
  File "/home/kostas89/google_appengine/google/appengine/ext/webapp/__init__.py", line 636, in __call__
    handler.post(*groups)
  File "/home/kostas89/library/library.py", line 59, in post
    book.put()
  File "/home/kostas89/google_appengine/google/appengine/ext/db/__init__.py", line 984, in put
    return datastore.Put(self._entity, config=config)
  File "/home/kostas89/google_appengine/google/appengine/api/datastore.py", line 455, in Put
    return _GetConnection().async_put(config, entities, extra_hook).get_result()
  File "/home/kostas89/google_appengine/google/appengine/datastore/datastore_rpc.py", line 629, in get_result
    self.check_success()
  File "/home/kostas89/google_appengine/google/appengine/datastore/datastore_rpc.py", line 599, in check_success
    rpc.check_success()
  File "/home/kostas89/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 558, in check_success
    self.__rpc.CheckSuccess()
  File "/home/kostas89/google_appengine/google/appengine/api/apiproxy_rpc.py", line 156, in _WaitImpl
    self.request, self.response)
  File "/home/kostas89/google_appengine/google/appengine/api/datastore_file_stub.py", line 710, in MakeSyncCall
    response)
  File "/home/kostas89/google_appengine/google/appengine/api/apiproxy_stub.py", line 87, in MakeSyncCall
    method(request, response)
  File "/home/kostas89/google_appengine/google/appengine/api/datastore_file_stub.py", line 793, in _Dynamic_Put
    self.__WriteDatastore()
  File "/home/kostas89/google_appengine/google/appengine/api/datastore_file_stub.py", line 643, in __WriteDatastore
    self.__WritePickled(encoded, self.__datastore_file)
  File "/home/kostas89/google_appengine/google/appengine/api/datastore_file_stub.py", line 699, in __WritePickled
    os.rename(tmp_filename, filename)
OSError: [Errno 21] Is a directory

So I 'm sure that this "way" (which i find in a forum) is totaly wrong. Any suggestion fow how i will put data and maybe how i will retrieve it (but this is another story)?
Thanks in advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬 2024-12-11 04:17:40

您应该为创建的实例分配值

class Add(webapp.RequestHandler):
  def post(self):
    book = Book() 
    book.title = self.request.get('title')
    book.author = self.request.get('author')
    book.category = self.request.get('category')        
    book.put()

You should assign values to the instance created

class Add(webapp.RequestHandler):
  def post(self):
    book = Book() 
    book.title = self.request.get('title')
    book.author = self.request.get('author')
    book.category = self.request.get('category')        
    book.put()
柳絮泡泡 2024-12-11 04:17:40

看起来您正在命令行上为 dev_appserver 指定数据存储路径,并且您指定的路径指向目录,而不是文件。尝试解决这个问题,或者完全删除争论。

It looks like you're specifying a datastore path on the command line to dev_appserver, and the path you're specifying points to a directory, not a file. Try fixing this, or removing the argument altogether.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文