1

I've been going over and over the Google documentation for GAE Python Blobstore:

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

and also some questions on the same

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?

But I'm not able to get the file upload and input fields to pass all in a single form to be processed (code and errors below) and it's also not getting into the 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 error:

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

It is not getting to the redirect page (going to HTTP error page). Also, the image key() is not getting into the Datastore value I set up for it

The handling is from the form to /upload using UploadHandler.

EDIT:

I realized some code was unrelated to these issues. Didn't change the question.

Community
  • 1
  • 1
stackuser
  • 869
  • 16
  • 34

1 Answers1

1

You need to use the value you get back from create_upload_url as the action for your form. Appengine will handle saving the blob for you, and then it will make the request to your /upload URL with the blobinfo and the remaining form parameters.

Greg
  • 10,350
  • 1
  • 26
  • 35
  • so yes, `
    ` seems to send the http by that field `p ------WebKitFormBoundaryFsj1etfaJVdOg63H Content-Disposition: form-data; name="uploadFile"; filename="bs-docs-twitter-github.png" Content-Type: image/png ` but it goes to a 404 page. and still nothing is in the blobstore. also the value in `BlogPosts` Datastore is showing `None` still.
    – stackuser Mar 07 '14 at 22:26
  • The URL will be a variable in your template, so should be wrapped with {{}} – Greg Mar 07 '14 at 23:47
  • ah yes of course. but it's still not getting into blobstore and the `image key()` field in datastore still reads `none`. thanks for your patience by the way. – stackuser Mar 07 '14 at 23:59
  • The entity you create with servePic = BlogPosts(imageTag = tag) never gets saved. You create another one with the other properties a few lines lower. – Greg Mar 08 '14 at 15:54
  • even `servePic = BlogPosts(imageTag = "anything")` didn't assign anything. adding it with the other datastore `put()` below it didn't work. is that why it's not getting into datastore, and how would you correct that? – stackuser Mar 08 '14 at 17:23