开发者

How to put data from a form in a datastore, using python - google app engine

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/goo开发者_运维百科gle_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.


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()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜