更新
请systempuntoout
查看评论。表单缺少提交按钮。现在一切正常。
我有这个代码,我认为会将图像写入数据存储区;但那并没有发生:
class AvatarSave(webapp.RequestHandler):
def post(self):
q = User.all()
q.filter("userEmail =", "az@example.com")
qTable = q.fetch(10)
if qTable:
for row in qTable:
avatar = images.resize(self.request.get("img"), 32, 32)
row.avatar = db.Blob(avatar)
db.put(qTable)
else:
self.response.out.write("user not found")
self.redirect('/')
日志显示图像键:
INFO 2010-12-04 13:56:26,601 dev_appserver.py:3317] "GET /img?
img_id=ag1oZWxsby0xLXdvcmxkcgsLEgRVc2VyGIABDA HTTP/1.1" 200 -
但除了断开的链接外没有显示任何内容。我在这里错过了什么?我很感激你的帮助。我在下面包含了整个脚本。谢谢!
class MainPage(webapp.RequestHandler):
def get(self):
siteUser = users.get_current_user()
greeting = None
if siteUser:
greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
(siteUser.nickname(), users.create_logout_url("/")))
else:
greeting = ("<a href=\"%s\">Sign in or register</a>" %
users.create_login_url("/"))
self.response.out.write(greeting)
self.response.out.write("""
<form action="/avatar-save" enctype="multipart/form-data" method="post">
<div><label>Avatar:</label></div>
<div><input type="file" name="img"/></div>
</form>
</body>
</html>""")
query = User.all()
query.filter("userEmail =", "az@example.com")
query.order("-userScore")
results = query.fetch(10)
self.response.out.write("""<html><head><style>
body {font-size: small;
font-family: Verdana, Helvetica, sans-serif;
}</style>
</head><body><ol>""")
for result in results:
self.response.out.write("<li>")
self.response.out.write("<b>%s</b> %s " % (result.userName, result.userLatestComment))
self.response.out.write("<div><img src='img?img_id=%s'></img>" % result.key())
self.response.out.write("</li>")
self.response.out.write("</ol></body></html>")
class Image (webapp.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 AvatarSave(webapp.RequestHandler):
def post(self):
q = User.all()
q.filter("userEmail =", "az@example.com")
qTable = q.fetch(10)
if qTable:
for row in qTable:
avatar = images.resize(self.request.get("img"), 32, 32)
row.avatar = db.Blob(avatar)
db.put(qTable)
else:
self.response.out.write("user not found")
self.redirect('/')
application = webapp.WSGIApplication(
[('/', MainPage),
('/img', Image),
('/avatar-save', AvatarSave),
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
答案 0 :(得分:3)
我认为您的代码缺少图片表单提交中的“提交”按钮:
self.response.out.write("""
<form action="/avatar-save" enctype="multipart/form-data" method="post">
<div><label>Avatar:</label></div>
<div><input type="file" name="img"/></div>
<input type="submit"/>
</form>...
我已尝试使用您的代码推断您未在问题中报告的User
模型:
class User(db.Model):
userEmail = db.EmailProperty()
userScore = db.IntegerProperty()
avatar = db.BlobProperty()
由于您的代码不包含创建用户的部分,因此我从开发交互式控制台创建了一个用户:
from main import User
User(userEmail='az@example.com', userScore=1).put()
然后,我已经使用az@example.com登录了,我已经上传了图片。
这是结果:
我只能说“它可以在我的机器上运行”。