This is a follow up question to my previous question. I am trying to upload and display an image as an avatar following the sample app here.
This is the code I am using:
In upload.py
I have:
from google.appengine.api import images
class Upload(webapp.RequestHandler):
def get(self):
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>""")
class AvatarSave(webapp.RequestHandler):
def post(self):
avatar = images.resize(self.request.get("img"), 32, 32)
greeting.avatar = db.Blob(avatar)
greeting.put()
self.redirect('/')
application = webapp.WSGIApplication(
[('/upload', Upload),
('/avatar-save', AvatarSave),
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
My first question is: How do I check that AvatarSave
actually saves the image to datastore?
I try to display the image in hw.py
:
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)
query = User.all()
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.w开发者_Python百科rite("No image")
application = webapp.WSGIApplication(
[('/', MainPage),
('/img', Image),
],
debug=True)
But all I get is the broken image link icon for each line displayed. (I have 8 lines displayed.)
In the Logs I see this:
INFO 2010-12-04 01:03:57,641 dev_appserver.py:3283] "GET / HTTP/1.1" 200 -
INFO 2010-12-04 01:03:57,703 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBMM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:57,756 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBUM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,734 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBIM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,812 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGFkM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,878 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGFsM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,934 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGFoM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,986 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGGkM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:59,040 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGGwM HTTP/1.1" 200 -
INFO 2010-12-04 01:04:00,102 dev_appserver.py:3283] "GET /favicon.ico HTTP/1.1" 200 -
As far as I understand, some images are fetched from the datastore; but I uploaded only 1 image (or I thought I did) but according to the logs the id for each image is different.
Can anyone help me understand what is going on here; and how to fix it? Many thanks for your help.
Update
In response to Ben's answer I updated the code like this:
This is the model I am using:
class User(db.Model):
userEmail = db.StringProperty()
....
avatar = db.BlobProperty()
and this is new AvatarSave
:
class AvatarSave(webapp.RequestHandler):
def post(self):
q = User.all()
q.filter("userEmail =", "az@example.com")
qTable = q.fetch(1)
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('/')
I believe this now updates the avatar of the user associated with the userEmail
"az@example.com"
. Is this correct?
I updated hw.py
to display only this user as well:
query = User.all()
query.filter("userEmail =", "az@example.com")
results = query.fetch(1)
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>")
But there is still a problem because instead of displaying the image, I get "None"
and the image link is broken.
Furthermore, when I look at the Datastore Viewer
, I don't see the "avatar"
column. But I see that some blob was updated at some point:
Entity Kind: __BlobUploadSession__
Key: ag10ZWxs....
ID: 125
Key Name:
creation: 1291388993.27
state: init
success_path: /upload
Not sure what is going on here and how to fix it. Any suggestion will be welcome.
In the log, you're seeing different image IDs because you're requesting up to 10 user objects from the Datastore, and the Datastore query is looking for the users regardless of whether or not they have an image stored (maybe you made 8 User objects in the course of testing).
As to why the avatars aren't saving:
class AvatarSave(webapp.RequestHandler):
def post(self):
avatar = images.resize(self.request.get("img"), 32, 32)
greeting.avatar = db.Blob(avatar)
Unless some code is missing from your example, the "greeting" variable is not set here. You have to first create a new greeting (by making a new instance of whatever model it uses) or load the appropriate one, then set the avatar to it, then greeting.put()
Update
If your code is indented like:
qTable = q.fetch(1)
if qTable:
for row in qTable:
avatar = images.resize(self.request.get("img"), 32, 32)
row.avatar = db.Blob(avatar)
db.put(qTable)
It will not work. What you want is probably:
qTable = q.fetch(1)
if qTable:
for row in qTable:
avatar = images.resize(self.request.get("img"), 32, 32)
row.avatar = db.Blob(avatar)
db.put(qTable)
But since you're working with only one object here, it would be simpler to replace your current use of fetch(1) and instead do:
currentUser = q.get()
if currentUser is not None:
avatar = images.resize(self.request.get("img"), 32, 32)
currentUser.avatar = db.Blob(avatar)
currentUser.put()
精彩评论