如何将图像写入数据存储?
更新:
请参阅systempuntoout
的评论。表单缺少提交按钮。现在一切正常了。
我有这段代码,我认为它会将图像写入数据存储区;但这并没有发生:
class AvatarSave(webapp.RequestHandler):
def post(self):
q = User.all()
q.filter("userEmail =", "[email protected]")
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 =", "[email protected]")
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 =", "[email protected]")
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()
UPDATE:
See the comment by systempuntoout
. The form was missing the submit button. Now it all works.
I have this code that I think would write an image to datastore; but that's not happenning:
class AvatarSave(webapp.RequestHandler):
def post(self):
q = User.all()
q.filter("userEmail =", "[email protected]")
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('/')
The log shows the image key:
INFO 2010-12-04 13:56:26,601 dev_appserver.py:3317] "GET /img?
img_id=ag1oZWxsby0xLXdvcmxkcgsLEgRVc2VyGIABDA HTTP/1.1" 200 -
But nothing is displayed except the broken link. What am I missing here? I would appreciate your help. I include the entire script below. Thanks!
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 =", "[email protected]")
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 =", "[email protected]")
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的代码缺少图像表单提交上的“提交”按钮:
我已尝试使用您的代码推断您在问题中未报告的
User
模型:因为您的代码不包含来自的部分创建用户,我从开发交互式控制台从头开始创建了一个用户:
然后,我使用 [电子邮件受保护],我已上传图片。
这是结果:
我只能说“它可以在我的机器上运行”。
I think your code is missing the Submit button on the image form submission:
I've tried your code inferring the
User
model that you have not reported in the question:since your code does not contain the part from creating the user, I have created a user from scratch from the Development interactive console with:
then, I've logged in with [email protected] and I've uploaded the picture.
This is the result:
All I can say is "it works on my machine".