2012-04-09 35 views
-1

我在Google應用引擎上很新穎。所以羅馬請幫我解決這個問題。明確地說,我的代碼是錯誤的,但我不能在哪裏?這裏的代碼:將文件上傳到blobstore數據存儲

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("[email protected]", emailto, "Confirmation Email", body) 
     mail.send_mail(sender="Daniyar Seisenov <[email protected]>", 
       to="Daniyar Seisenov <[email protected]>", 
       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。請幫助我,如果你知道什麼是解決方案。謝謝

回答

6

這有點令人困惑,但數據存儲區和blobstore是應用程序引擎中的兩個獨立的存儲機制。 BlobProperty在數據存儲中存儲blob,而不是在blobstore中。

它看起來像你的代碼會把一個文件放到BlobStore中,但是你的MyUsers.file屬性需要存儲BlobStore鍵。

另一種選擇是將blob實際存儲在數據存儲中,在這種情況下,您不會使用BlobstoreUploadHandler,而是必須自己將對象從POST數據中取出,並將其存儲在MyUsers .file字段。

+0

感謝您的回覆。你不認爲我的問題可能也在這裏:self.redirect('/服務/%s'%blob_info.key())在我的模板頁面操作是:(/ register)..我試圖用%s替換/註冊,但它不工作..你會介意我該怎麼處理&s嗎?再次感謝 – Danik 2012-04-10 18:15:28

+0

那麼你想要將文件存儲在Blobstore中或作爲數據存儲中的一個blob?首先,你需要弄清楚。 Youre Register()函數將文件保存在數據存儲中,但ServeHandler()嘗試從Blobstore檢索它。 – dragonx 2012-04-10 19:31:04

+0

我想將我的文件存儲在Blobstore中。將不勝感激,如果你能幫助我 – Danik 2012-04-10 23:54:51

相關問題