db.get() will grab an entity of _any_ kind. Model.get checks that the key is for the correct kind of entity before fetching it. Either will work. When I use db.get I usually include a check to make sure the keys are the correct kind.
The key he posted in his initial thread is a Photo. Robert On Tue, Sep 14, 2010 at 11:34, John McLaughlin <[email protected]> wrote: > The one thing that looked off to me is that the line in ServePhoto > > photo = db.get(self.request.get("photo_id")) > > might want to be > > photo = Photo.get(self.request.get("photo_id")) > > I think db.get is a datastore method, not an inherited method from > db.Model > > > On Sep 13, 8:09 pm, Robert Kluin <[email protected]> wrote: >> I glanced over your code, nothing really major jumped out at me. If >> you are not getting an exception in ServePhoto then the model is >> clearly getting created and successfully fetched. >> >> So, here are my first thoughts: >> Have you tried logging some debug info in the UploadPhoto and >> ServePhoto handlers? Specifically are you sure data is actually in >> the photo property and it is not just an empty string or something? >> Maybe you could try logging len(photo.photo) right before saving it, >> then again right after fetching it. >> >> What happens when you remove the if and simply return photo.photo? >> >> Robert >> >> class ServePhoto (webapp.RequestHandler): >> def get(self): >> photo = db.get(self.request.get("photo_id")) >> if photo.photo: >> self.response.headers['Content-Type'] = "image/jpg" >> self.response.out.write(photo.photo) >> else: >> self.error(404) >> >> ------ >> Robert Kluin >> Ezox Systems, LLC >> >> On Sun, Sep 12, 2010 at 22:03, Raymond >> >> >> >> <[email protected]> wrote: >> > Hi All, >> >> > I have been trying to understand how to upload and download images >> > from to a Blobstore and have hit a wall. >> > I have essentially made a modified version of the Guestbook example >> > provided by google and modified it to suit my needs, I have tested the >> > guestbook app as is and got it working, somehow somewhere I am making >> > a simple mistake that frustrate every attempt at getting it working in >> > my own code. >> > I have stripped down my code to the essential in the hope of >> > understanding what is wrong, but I am still stuck. >> >> > What am I attempting to do ? >> > Upload in a blobstore an image and display it in a web page. >> >> > What is my code ? >> >> > 1) My upload form served from a static page >> >> > ... >> > <form action="http://192.168.0.196:8083/uploadphoto" >> > enctype="multipart/form-data" method="post"> >> > <div><label>Attempt at uploading a picture using a form</label></ >> > deiv> >> > <div><input type="file" name="photo" /></div> >> > <div><input type="submit" value="Upload Photo" /></div> >> > </form> >> > ... >> >> > 2) My db model : >> >> > class Photo(db.Model): >> > photo = db.BlobProperty() >> > date = db.DateTimeProperty(auto_now_add=True) >> >> > 3) The code uploading the photo and saving it in the blobstore >> >> > class UploadPhoto(webapp.RequestHandler): >> > def post(self): >> > photo = Photo() >> > img = self.request.get('photo') >> > photo.photo = db.Blob(img) >> > photo.put() >> >> > 4) The code creating the web page displaying the images : >> >> > class PhotoPage(webapp.RequestHandler): >> > def get(self): >> > self.response.out.write('<html><pre>') >> > self.response.out.write('<h1>Date&Time Photo</h1>') >> > photos = db.GqlQuery("SELECT * FROM Photo ORDER BY date >> > DESC LIMIT >> > 10") >> > for photo in photos: >> > self.response.out.write('<p>%s ' % photo.date) >> > self.response.out.write('<img >> > src="servephoto?photo_id=%s" /></p>' >> > % photo.key()) >> > self.response.out.write('</pre></body></html>') >> >> > 5) The code serving the images : >> >> > class ServePhoto (webapp.RequestHandler): >> > def get(self): >> > photo = db.get(self.request.get("photo_id")) >> > if photo.photo: >> > self.response.headers['Content-Type'] = "image/jpg" >> > self.response.out.write(photo.photo) >> > else: >> > self.error(404) >> >> > Everything seem to work until this last stage, I can select an image >> > in my form, upload it, Something is written in Binary in the >> > Blobstore. >> > When I visit the page supposed to display the image I see all info but >> > a broken image icon. >> > I checked the source code and the HTML seem to be just fine, here it >> > is with one record in the blobstore, I get the blob key which mena >> > that there is a record with some binary stuff in it. >> >> > <html><pre><h1>Date&Time Photo</h1><p>2010-09-13 00:31:14.477698 >> > <img >> > src="servephoto?photo_id=agt0ZXN0cmF5c3Bvc3ILCxIFUGhvdG8YWww" /></p></ >> > pre></body></html> >> >> > I also know that the ServePhoto class is called and executed but it if >> > photo.photo always return false and execute self-eror(404). >> > I tried replacing this with a different error code and it always >> > display the error code so I am sure the if statement get a false. >> >> > My questions is, why would the if statement get a false if there is >> > effectively an image in the blobstore (Proved by the fact that I can >> > retrieve it's key) ? >> >> > I am sorry for the long post, and hope I am not too confusing. >> >> > Thanks for any hint that would get me on the right way and forgive me >> > if the answer is obvious, I probably need new eyes. >> >> > Raymond >> >> > -- >> > You received this message because you are subscribed to the Google Groups >> > "Google App Engine" group. >> > To post to this group, send email to [email protected]. >> > To unsubscribe from this group, send email to >> > [email protected]. >> > For more options, visit this group >> > athttp://groups.google.com/group/google-appengine?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Google App Engine" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-appengine?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
