0

我遇到訪問數據存儲實體的問題。我是GAE和Python的新手,所以要善良。我一直在尋找來自在線教程和其他示例代碼的代碼,到現在爲止這一直很順利。GAE呈現「無」,但我可以在數據存儲查看器中看到數據存儲實體

最終,我希望能夠剝離電子郵件附件,對文件進行一些簡單的更改並與其他.kml文件(或更好的.kmz文件)合併(但是一次只能執行一項操作: )。

step1。是發送電子郵件到應用程序的appspotmail地址=完成:)

step2。 ...寫入到db.model =我可以在儀表板/數據存儲區查看器中看到實體在那裏,因此沒有問題=完成:)

step3。 - 呈現一個模板以顯示最近收到的20封電子郵件。這個名單將動態更新..... :(Hmmmmm

反而會發生什麼情況是,我看到index.html中呈現的所有變量none。可以或許點我在正確的方向?謝謝!

DAV-O

這裏是yaml.app

application: racetracer 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: false 

libraries:                  
- name: jinja2                 
    version: latest 

handlers: 
- url: /_ah/mail/.+ 
    script: handle_incoming_email.py 
    login: admin 

- url: /favicon.ico 
    static_files: images/favicon.ico 
    upload: images/favicon.ico 

- url: /static/css 
    static_dir: static/css 

- url: /static/html 
    static_dir: static/index.html 

- url: .* 
    script: main.app 

inbound_services: 
- mail 

這裏是handle_incoming_email.py

import logging, email 
import cgi 
import datetime 
import os.path 
import os 

from os import path 
from google.appengine.ext import webapp 
from google.appengine.ext import db 
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 
from google.appengine.ext.webapp.util import run_wsgi_app 
from google.appengine.ext.webapp.template import render 
import jinja2 
import wsgiref.handlers 

from main import * 
from baseController import * 
from authController import * 

class Vault(db.Model): 
    #fromAddress= db.UserProperty() 
    fromAddress= db.StringProperty() 
    subject = db.StringProperty(multiline=True) 
    #date = db.DateTimeProperty(auto_now_add=True) 
    date = db.StringProperty(multiline=True) 
    name = db.StringProperty(multiline=True) 

class ProcessEmail(InboundMailHandler): 
    def receive(self, mailMessage): 
     logging.info("Email from: " + mailMessage.sender + " received ok") 
     logging.info("Email subject: " + mailMessage.subject) 
     logging.info("Email date: " + mailMessage.date) 

     fromAddress = mailMessage.sender 
     subject = mailMessage.subject 
     date = mailMessage.date 

     if not hasattr(mailMessage, 'attachments'): 
      logging.info("Oi, the email has no attachments") 
      # raise ProcessingFailedError('Email had no attached documents') 
     else: 
      logging.info("Email has %i attachment(s) " % len (mailMessage.attachments)) 

      for attach in mailMessage.attachments: 
       name = attach[0] #this is the file name 
       contents = str(attach[1]) #and this is the attached files contents 
       logging.info("Attachment name: " + name) 
       logging.info("Attachment contents: " + contents) 
       vault = Vault() 
       vault.fromAddress = fromAddress 
       vault.subject = subject 
       vault.date = date 
       vault.name = name 
       vault.contents = contents #.decode() EncodedPayload.decode() 
        vault.put()  

       #f = open("aardvark.txt", "r") 
       #blah = f.read(30); 
       #logging.info("the examined string is : " + blah) 
       #f.close() # Close opened file 

app = webapp.WSGIApplication([ 
    ProcessEmail.mapping() 
], debug=True) 

def main(): 
    run_wsgi_app(app) 

if __name__ == "__main__": 
    main() 

,這裏是basecontroller.py

from authController import * 
from handle_incoming_email import * 
import logging 
import os 
import webapp2 
import jinja2 
import wsgiref.handlers 

from google.appengine.ext.webapp import template 
from google.appengine.ext import db 
from google.appengine.api import users 

TEMPLATE_DIR = os.path.join(os.path.dirname(__file__)) 
jinja_environment = \ 
    jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_DIR)) 

class Vault(db.Model): 
    #fromAddress= db.UserProperty() 
    fromAddress= db.StringProperty() 
    subject = db.StringProperty(multiline=True) 
    #date = db.DateTimeProperty(auto_now_add=True) 
    date = db.StringProperty(multiline=True) 
    name = db.StringProperty(multiline=True) 

class MainPage(webapp2.RequestHandler):  
    def get(self): 
     logging.info("this is MainPage in baseController") 
     list = db.GqlQuery("SELECT * FROM Vault ORDER BY date DESC").fetch(10) 
     logging.info("this is in list: " + str(list)) 

     vault = Vault() 

     fromAddress = vault.fromAddress 
     subject = vault.subject 
     date = vault.date 
     name = vault.name 

     values = { 
       'fromAddress': fromAddress, 
      'subject': subject, 
      'date': date, 
      'name': name, 
      } 
     templ = jinja_environment.get_template('index.html') 
     self.response.out.write(templ.render(values))       

def main(): 
    wsgiref.handlers.CGIHandler().run(app) 

if __name__ == "__main__": 
    main() 

我在教程中讀到呈現模板是一種優於只使用在的.py的HTML,但也許我在太多的文件碎下來?他們的邏輯是不同的人做前端和後端,所以現在就習慣。反正上面有意渲染的index.html是:

的index.html

{% extends "base.html" %} 
{% block title %} racetracer {% endblock %} 
{% block content %} 
<h1>Racetracer </h1> 
<h3>Files recently received :</h3> 
<br> 
     <table> 
     <tr> 
      <th>Email address:     </th> 
      <th>-----------      </th> 
      <th>Subject:      </th> 
      <th>Date Received:     </th> 
      <th>Attachment:      </th> 
     </tr>   
     <tr>     
      <td> {{ fromAddress }}    </td> 
      <td> ---------------    </td> 
      <td> {{ subject }}     </td> 
      <td> {{ date }}      </td> 
      <td> {{ name }}      </td> 
     <blockquote>{{ content|escape }}</blockquote>      
     </p> 
     </tr>        
    </tr>  
    </table>  
<br> 

<hr> 
<form action="/sign" method="post"> 
    <textarea name="content" rows="1" cols="20"></textarea> 
    <input type="submit" value="Submit (in index:)"> 
</form> 
{% endblock %} 

和base.html文件....

<html><head> 
<link type="text/css" rel="stylesheet" href="/static/css/main.css" /></head> 

<body> From the file base out in the racetracer folder<br> 
right now you are logged in as : 
    {% if user %} 
     {{ user.nickname }}! 
     [<a href="{{ logout }}"><b>sign out</b></a>] 
    {% else %} 
     anonymous, who are you? 
     [<a href="{{ login }}"><b>sign in</b></a>] 
    {% endif %} 
    {% block content %} 
    {% endblock %} 
</body></html> 

,這裏是main.py

import os 
import webapp2 
from handle_incoming_email import * 
from baseController import * 
from authController import * 
from google.appengine.ext.webapp import template 

app = webapp2.WSGIApplication([ 
    ('/', MainPage), 
    #('/ProcessEmail', ProcessEmail), 
], debug=True) 

def main(): 
    wsgiref.handlers.CGIHandler().run(app) 

if __name__ == "__main__": 
    main() 

感謝您的幫助!這將非常感激。

+0

您應該將模型移動到單個文件中並將其導入到不同的處理程序中,因爲您有2個獨立的Vault實現,所以很容易引入各種錯誤。 – 2013-03-26 06:48:21

+0

好的,謝謝蒂姆。所以我會創建一個model.py文件,它只是由'class vault'組成,因爲它是上面的,並且在需要時添加'from model.py import *',是的?不知道這會改變任何渲染問題,雖然??? – user2206361 2013-03-26 11:38:15

+0

是的,我沒有解決你的實際問題,以通過:-)的很多代碼,但複製類作爲一個普遍的問題等待咬人:-) – 2013-03-26 12:28:35

回答

1

你得到None,因爲它是None。您聲明的是Vault對象,但不會爲其分配任何值。作爲GQL查詢的結果,這些值將位於列表實體中。

vault = Vault() # This creates an empty object (assuming you aren't doing any dynamic init 

fromAddress = vault.fromAddress # Still empty.... 
subject = vault.subject 
date = vault.date 
name = vault.name 

此外,你不應該指定列表作爲一個變種,因爲這是保留給Py。

你想要做的是設置這樣的:

my_list = YOUR GQL QUERY.fetch() 

# Create a formatted list 
values = [] 
for ml in my_list: 
    value = { 
      'fromAddress': ml.fromAddress, 
     'subject': ml.subject, 
     'date': ml.date, 
     'name': ml.name, 
     } 

    values.append(value) # You could just append the dictionary directly here, of course 

然後使用值列表中你模板PARAMS

** **更新相比

你GQL查詢看起來不錯到您的數據存儲庫模型。

首先,確保您已將'list'var更改爲'my_list'。 接下來,如果你想看到打印爲可讀的字符串檢索對象的內容,這 添加到您的模型:

def __unicode__(self): 
     return ('%s %s %s' % (self.key,self.fromAddress,self.subject)) # etc... for the vars you want printed with you print the object 

檢查logging.info(my_list),並查看是否打印任何東西更具可讀性。

如果沒有,該列表上運行一個for循環並記錄密鑰和/或FROMADDRESS,因此:

for vault in my_list: 
    logging.info('the key = %s'%vault.key) 

如果不是返回任何東西,你的開發環境和運行中直接進入交互式控制檯該查詢:

from google.appengine.ext import db 
from *vault_file* import Vault 

my_list = db.GqlQuery("SELECT * FROM Vault ORDER BY date DESC").fetch(10) # run query 

for vault in my_list: 
    print vault.key 
    print vault.fromAddress 

讓我知道這些測試離開你的位置,我會繼續更新。

*更新#2 *

所以,現在你能確認你獲得的數據存儲值
要設置一個模板頁面參數等於保管庫列表中,這樣:

params = dict(values=my_list) 
self.response.out.write(templ.render(params)) 

現在在您的頁面上,您需要遍歷列表以打印每個字典項目:

{% for vault in values %} 
    <tr> 
     <td>{{vault.fromAddress}}}</td> 
     etc... 
    </tr> 

{% endfor %} 

那工作?

+0

謝謝凱文。我按照你所說的改變了代碼,但是html頁面上沒有任何東西(甚至沒有來自以前的'none')。有一件事,我在'my_list = db.GqlQuery(「SELECT * FROM Vault ORDER BY date DESC」)中注意到了。在日誌中這行產生'INFO 2013-03-26 22:32:24,828 baseController.py:36]這是在列表:[]'。這是否意味着Vault是正確寫入的?你認爲我應該在哪裏看下一個?再次感謝。 Dav-o – user2206361 2013-03-26 22:36:57

+0

AND ....當我部署應用程序,然後看日誌,我得到'這是在my_list:[,...等等。那麼,如何最好地將它們轉換爲字符串? – user2206361 2013-03-26 22:52:53

+0

你的日誌看起來像你的查詢返回沒有結果,所以讓我們開始。你能證實你在數據存儲中有什麼嗎? – kevin 2013-03-27 01:04:36

相關問題