1

由于我从 django(和 django-nonrel)切换到 webapp2,我在理解和处理 Blobstore/Datastore 上传时遇到了一些困难。

我的应用程序结构中有handlers.py,forms.py models.pyviews.py一个模板文件夹。

我得到一个列表索引超出范围错误,我也有一些其他的错误,但是在玩了一点代码之后这些就消失了。我不确定我是否理解得很好,但一些资源说它需要重定向。

我希望表单字段转到数据存储区,并将文件作为 blob,其中 blob 密钥将保存在数据存储区中。这是在handlers.py

class AddSubmissionHandler(blobstore_handlers.BlobstoreUploadHandler, webapp2.RedirectHandler):

def get(self):
    upload_url = blobstore.create_upload_url('/submissions/add/')


def post(self):

    candidate = Candidate()
    candidate.name = self.request.get('name')
    candidate.email = self.request.get('email')
    candidate.phone = self.requeforms.pyst.get('phone')
    candidate.location_name = self.request.get('location_name')
    candidate.referred_by = self.request.get('referred_by')
    candidate.cover_page = self.request.get('cover_page')

    # 'file' is file upload field in the form
    try:
        upload_files = self.get_uploads('resume_file')
        blob_info = upload_files[0]
        candidate.put()
    finally:

        self.redirect('submissions/')

这是在forms.py

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'])
forms.py

class AddSubmissionForm(webapp2.RedirectHandler):

    def get(self):
        upload_url = blobstore.create_upload_url('/submissions/add/')

        template = JINJA_ENVIRONMENT.get_template('templates/submission_form.html')
        self.response.write(template.render())

这是处理表单的实际模板

<!DOCTYPE html>
<html>
<head>
    <title>Resume submission</title>
</head>
<body>
   <form action="{{upload_url}}" method="post" enctype="multipart/form-data">
       <div>Name:<input name="name" value=""/></div>
       <div>Email:<input name="email" value=""/></div>
       <div>Phone:<input name="phone" value=""/></div>
       <div>Your Location:<input name="location_name" value=""/></div>
       <div>Referred by:<input name="referred_by" value=""/></div>
       <div>Cover Page:<textarea name="cover_page" rows="5" cols="60"></textarea></div>
       <div>Resume File: <input type="file" name="resume_file" size="40"></div>
       <div><input type="submit" value="Apply!"></div>
    </form>

</body>
</html>

追溯给出

INFO     2013-06-26 11:00:21,595 server.py:585] default: "POST /submissions/add/ HTTP/1.1" 500 1598
ERROR    2013-06-26 11:01:08,760 webapp2.py:1552] list index out of range
Traceback (most recent call last):
  File "/home/borko/Desktop/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/borko/Desktop/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, re        blob_info = upload_files[0]sponse)
  File "/home/borko/Desktop/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/borko/Desktop/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/home/borko/Desktop/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/home/borko/Desktop/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/home/borko/gae/submissions/handlers.py", line 26, in post
    blob_info = upload_files[0]
IndexError: list index out of range
4

0 回答 0