Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3450582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:03:57+00:00 2026-05-18T09:03:57+00:00

This is a follow up question to my previous question . I am trying

  • 0

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.write("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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:03:58+00:00Added an answer on May 18, 2026 at 9:03 am

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.