我遇到訪問數據存儲實體的問題。我是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()
感謝您的幫助!這將非常感激。
您應該將模型移動到單個文件中並將其導入到不同的處理程序中,因爲您有2個獨立的Vault實現,所以很容易引入各種錯誤。 – 2013-03-26 06:48:21
好的,謝謝蒂姆。所以我會創建一個model.py文件,它只是由'class vault'組成,因爲它是上面的,並且在需要時添加'from model.py import *',是的?不知道這會改變任何渲染問題,雖然??? – user2206361 2013-03-26 11:38:15
是的,我沒有解決你的實際問題,以通過:-)的很多代碼,但複製類作爲一個普遍的問題等待咬人:-) – 2013-03-26 12:28:35