-1

我对 Google 应用引擎很陌生。所以请大家帮我解决这个问题。明确地说,我的代码是错误的,但我不知道它在哪里?这里代码:

import os
import urllib

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp.util import run_wsgi_app
import datetime
import wsgiref.handlers
from google.appengine.api import mail
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp \
    import template


class MyUsers(db.Model):
    firstname = db.StringProperty(
                    required=False)
    lastname = db.StringProperty(
                    required=False)
    email = db.StringProperty(
                    required=False)
    zipcode = db.StringProperty(
                    required=False)
    file = db.BlobProperty(
                    required=False)
    idea = db.TextProperty(
                    )
    ask = db.StringProperty(
                    required=False)
    contact_back = db.StringProperty(
                    required=False)
    phone = db.StringProperty(
                    required=False)
    regdate = db.DateTimeProperty(
                    auto_now_add=True)

class MainHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write(
            template.render('expert.html',[]))


class Register(webapp.RequestHandler):
    def get(self):
            upload_url = blobstore.create_upload_url('/upload')
            self.response.out.write(
            template.render('expert.html',[]))

    def post(self):
        user = MyUsers(
            firstname = self.request.get('firstname'),
            lastname = self.request.get('lastname'),
            email = self.request.get('email'),
            zipcode = self.request.get('zipcode'),
            ask = self.request.get('ask'),
            idea = self.request.get('idea'),
            file = self.request.get('file'),
            contact_back = self.request.get('contact_back'),
            phone = self.request.get('phone'),
            )
        phone = self.request.get('phone')
        firstname = self.request.get('firstname')
        email = self.request.get('email')
        lastname = self.request.get('lastname')
        user.put()

        emailto = self.request.get('email')
        body = """Dear """+firstname+""":

        Thank you for registering.

        Please let us know if you have any questions.

        As One Source.
        """
        mail.send_mail("daniyar.seisenov@gmail.com", emailto, "Confirmation Email", body)
        mail.send_mail(sender="Daniyar Seisenov <daniyar.seisenov@gmail.com>",
              to="Daniyar Seisenov <daniyar.seisenov@gmail.com>",
              subject="New User",
              body="""
        This applicant has just registered. Please review:

        Firstname: """+firstname+"""
        Lastname: """+lastname+"""
        Email: """+email+"""
        Phone: """+phone+"""



        """)
        self.redirect('/')



class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
        blob_info = upload_files[0]

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)        
        self.redirect('/serve/%s' % blob_info.key())

class Users(webapp.RequestHandler):
    def get(self):
        myusers=db.GqlQuery(
            'SELECT * FROM MyUsers '
            'ORDER BY regdate DESC'
            )
        values = {
            'myusers':myusers,
            'totalmyusers' : myusers.count()
        }

        self.response.out.write(
            template.render('userscountandremove.html',
                            values))

def main():
    application = webapp.WSGIApplication([('/', MainHandler),
                                        ('/upload', UploadHandler),
                                        ('/serve/([^/]+)?', ServeHandler),
                                          ('/register',Register),
                                          ('/userscountandremove',Users)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()

当我从expert.html 按下提交时,“文件”不会进入数据存储中的blobstore。如果您知道什么是解决方案,请帮助我。谢谢

4

1 回答 1

6

这有点令人困惑,但数据存储和 blobstore 是应用引擎中的两个独立存储机制。BlobProperty 将 blob 存储在数据存储中,而不是存储在 blobstore 中。

看起来您的代码会将文件放入 BlobStore,但您的 MyUsers.file 属性需要存储 BlobStore 密钥。

另一种选择是将 blob 实际存储在数据存储中,在这种情况下,您不会使用 BlobstoreUploadHandler,而是必须自己从 POST 数据中提取对象,并将其存储在 MyUsers.file 字段中。

于 2012-04-09T23:51:19.703 回答