【问题标题】:Python Google App Engine -- multipart form input Blobstore and DatastorePython Google App Engine -- 多部分表单输入 Blobstore 和 Datastore
【发布时间】:2014-04-11 06:31:30
【问题描述】:

我一直在翻阅 GAE Python Blobstore 的 Google 文档:

https://developers.google.com/appengine/docs/python/blobstore/

还有一些关于相同的问题

Google App Engine (Python) - Uploading a file (image)

Html form submit both datastore entity and blobstore through app engine

How to upload a file using the BlobStore in Python App Engine?

但我无法让文件上传和输入字段以单个表单传递所有内容以进行处理(代码和错误如下),并且它也没有进入 Blobstore。

newpost.html

<form method="post" action="/upload" role="form" enctype="multipart/form-data">
  <div class="col-xs-6">
   <div class="form-group">
    <label for="title"><h2><small>Enter Title of this Post</small></h2>
    <input type="text" name="subject" value="{{ subject }}" class="form-control">
    </label>
   </div>
  </div>
   <div class="pull-right">

  <div class="form-group">
   <label for="exampleInputFile">File upload (Optional)</label>
     <input type="file" id="uploadFile" name="uploadFile">
   <p class="help-block">
   You are responsible for copyrighted or inappropriate files that you upload.</p>
  </div>

<hr><hr>
    <img src="../img/powered_by.png" width="200" alt="logo" class="img-rounded">
   </div>
   <div class="col-xs-6">
     <label for="art"><h2><small>Enter Post</small></h2></label>
      <div class="input-group">
        <textarea name="content" class="form-control input-lg">{{ content }}</textarea>
        <span class="input-group-addon input-group-btn">
        <button type="submit" class="btn btn-danger btn-lg">Submit</button>
        </span>
      </div>
     <div style="color:red">{{ error }}</div>
   </div>
</form>

weblog.py

class PixHandler(webapp2.RequestHandler):
  def get(self):
    upload_url = blobstore.create_upload_url('/upload')
##    self.response.out.write('<html><body>')
##    self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
##    self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit"
##        name="submit" value="Submit"> </form></body></html>""")

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
  def post(self):
    upload_files = self.get_uploads('uploadFile')  # from upload field in the form
    blob_info = upload_files[0]
    tag = '/pic/%s' % blob_info.key()
    servePic = BlogPosts(imageTag = tag) # BlobStore entry

    subject = self.request.get("subject")
    content = self.request.get("content")
    query = self.request.get("rname")
    if (query):
        self.render_query()
    elif subject and content:
        msg = "Looks good"
        entry = BlogPosts(subject = subject, content = content)
        entry_key = entry.put()
        permalink = entry_key.id()
        self.redirect("./%s" % str(permalink))
    elif (not subject and not content):
        msg = "Neither box can be empty"
        self.render_html(subject, content, msg)

    self.redirect(tag) # serves pix with unique id in url

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
  def get(self, resource):
    resource = str(urllib.unquote(resource))
    blob_info = blobstore.BlobInfo.get(resource)
    self.send_blob(blob_info)

HTTP 错误:

File "C:\Python27\udacity-web-app-rs-1\weblog.py", line 101, in post
    blob_info = upload_files[0]
IndexError: list index out of range

它没有进入重定向页面(进入 HTTP 错误页面)。此外,image key() 没有进入我为其设置的 Datastore 值

处理是使用UploadHandler从表单到/upload

编辑:

我意识到有些代码与这些问题无关。没有改变问题。

【问题讨论】:

    标签: python google-app-engine python-2.7 google-cloud-datastore blobstore


    【解决方案1】:

    您需要使用从create_upload_url 返回的值作为表单的action。 Appengine 将为您处理保存 blob,然后它将使用 blobinfo 和其余表单参数向您的 /upload URL 发出请求。

    【讨论】:

    • 所以是的,&lt;form method="post" action=upload_url role="form" enctype="multipart/form-data"&gt; 似乎通过该字段 p ------WebKitFormBoundaryFsj1etfaJVdOg63H Content-Disposition: form-data; name="uploadFile"; filename="bs-docs-twitter-github.png" Content-Type: image/png 发送 http,但它转到 404 页面。并且在 blobstore 中仍然没有任何内容。 BlogPosts Datastore 中的值也仍然显示None
    • URL 将是模板中的变量,因此应使用 {{}}
    • 啊,当然。但它仍然没有进入 blobstore,并且数据存储中的 image key() 字段仍然显示为 none。顺便感谢您的耐心等待。
    • 您使用 servePic = BlogPosts(imageTag = tag) 创建的实体永远不会被保存。你再创建一个,其他属性低几行。
    • 甚至servePic = BlogPosts(imageTag = "anything") 也没有分配任何东西。将它与下面的其他数据存储 put() 添加它不起作用。这就是它没有进入数据存储区的原因,您将如何纠正?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2014-03-06
    • 1970-01-01
    相关资源
    最近更新 更多