我正在尝试使用带有Python的google-app-engine上传图片。我试过https://developers.google.com/appengine/docs/python/images/usingimages的例子
任何人都可以解释我如何使用谷歌应用程序引擎上传图像?
谢谢!
日志:
在__ init __中输入文件“C:\ Program Files \ Google \ google_appengine \ google \ appengine \ api \ datastore_types.py”,第382行
raise datastore_errors.BadKeyError('无效的字符串键%s。'%encoded)
BadKeyError:无效的字符串键
守则:
import cgi
import datetime
import urllib
import webapp2
from google.appengine.ext import db
from google.appengine.api import images
from google.appengine.api import users
class Greeting(db.Model):
"""Models a Guestbook entry with an author, content, avatar, and date."""
author = db.StringProperty()
content = db.StringProperty(multiline=True)
avatar = db.BlobProperty()
date = db.DateTimeProperty(auto_now_add=True)
def guestbook_key(guestbook_name=None):
"""Constructs a Datastore key for a Guestbook entity with guestbook_name."""
return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
guestbook_name=self.request.get('guestbook_name')
greetings = db.GqlQuery('SELECT * '
'FROM Greeting '
'WHERE ANCESTOR IS :1 '
'ORDER BY date DESC LIMIT 10',
guestbook_key(guestbook_name))
for greeting in greetings:
if greeting.author:
self.response.out.write(
'<b>%s</b> wrote:' % greeting.author)
else:
self.response.out.write('An anonymous person wrote:')
self.response.out.write('<div><img src="img?img_id=%s"></img>' %
greeting.key())
self.response.out.write('<blockquote>%s</blockquote></div>' %
cgi.escape(greeting.content))
self.response.out.write("""
<form action="/sign?%s" enctype="multipart/form-data" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><label>Avatar:</label></div>
<div><input type="file" name="img"/></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<hr>
<form>Guestbook name: <input value="%s" name="guestbook_name">
<input type="submit" value="switch"></form>
</body>
</html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}),
cgi.escape(guestbook_name)))
class Image(webapp2.RequestHandler):
def get(self):
greeting = db.get(self.request.get('img_id'))
if greeting.avatar:
self.response.headers['Content-Type'] = 'image/png'
self.response.out.write(greeting.avatar)
else:
self.response.out.write('No image')
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name')
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = users.get_current_user().nickname()
greeting.content = self.request.get('content')
avatar = images.resize(self.request.get('img'), 32, 32)
greeting.avatar = db.Blob(avatar)
greeting.put()
self.redirect('/?' + urllib.urlencode(
{'guestbook_name': guestbook_name}))
app = webapp2.WSGIApplication([('/', MainPage),
('/img', Image),
('/sign', Guestbook)],
debug=True)
答案 0 :(得分:0)
你正在使用类似金雅的参数,但你没有填充任何模板,而是用python格式化一个字符串:
self.response.out.write("""
<form action="/sign?%s" enctype="multipart/form-data" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><label>Avatar:</label></div>
<div><input type="file" name="img"/></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<hr>
<form>Guestbook name: <input value="%s" name="guestbook_name">
<input type="submit" value="switch"></form>
</body>
</html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}),
cgi.escape(guestbook_name)))
请注意,您说urrlib.urlenconde({'guestbook_name': guestbook_name})
其中python只需要一个字符串:guestbook_name
。
此外,假设您对所有占位符使用相同的值,则可以使用
"...<input value="%(guestbook)s" name..." % {'guestbook': cgi.escape(guestbook_name)})
或
"...<input value="{0}" name... ...</html>".format(cgi.escape(guestbook_name))
希望它有所帮助。