Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle delete in Google App Engine (Python)
    primarykey
    data
    text
    <p>I'm a newbie programmer and new to Google App Engine and webapp2 etc. So this may be a very basic question.</p> <p>I am creating an application to store images into BlobStore. My model stores description, blob_key, image url and date.</p> <p>I am able to save everything, so that bit is okay. </p> <p>But now I want to create a delete button which will not only delete an item from the datastore, but also delete the image saved in the blobstore.</p> <p>I have created a DeleteHandler, and in the html I have a form, passing the key for the item I want to delete. In the DeleteHandler, I am using the posted key to delete the item from the datastore. I am also trying to use the key to use it delete the image saved in the blobstore.</p> <p>So far I'm getting a 404 on the delete form post, and even if I get past that, I'm not sure if my DeleteHandler is correct to handle the functionality I am looking for.</p> <p>Any help would be much appreciated..</p> <p>Main.py:</p> <pre><code>import os import urllib import webapp2 from google.appengine.ext.webapp import template from google.appengine.ext import blobstore from google.appengine.ext.webapp import blobstore_handlers from google.appengine.api import images #Models from google.appengine.ext import db class ImageItem(db.Model): description = db.StringProperty(required=True) img_url = db.StringProperty() blob_key = blobstore.BlobReferenceProperty() when = db.DateTimeProperty(auto_now_add=True) #Handlers (Views) class MainHandler(webapp2.RequestHandler): def get(self): upload_url = blobstore.create_upload_url('/upload') imgs = db.GqlQuery( 'SELECT * FROM ImageItem ' 'ORDER BY when DESC') imgs_dict = {'imgs': imgs} self.response.out.write( template.render( 'main.html',locals() ) ) class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): def post(self): f = self.get_uploads('file')[0] # 'file' is file upload field in the form img =ImageItem(description=self.request.get('description')) img.blob_key = f.key() img.img_url = images.get_serving_url( f.key() ) img.put() self.redirect('/') class DeleteHandler(webapp2.RequestHandler): def post(self): key = self.request.get('k') item = db.get(key) images.delete( item.blob_key ) item.delete() self.response.out.write(key) #URL Routing happens here app = webapp2.WSGIApplication([('/', MainHandler), ('/upload', UploadHandler), ('/delete', DeleteHandler)], debug=True) </code></pre> <p>Main.html:</p> <pre><code>&lt;form action="{{upload_url}}" method="POST" enctype="multipart/form-data"&gt; &lt;p&gt; &lt;label for="file"&gt;Upload File&lt;/label&gt; &lt;input type="file" name="file" id="file"&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="description"&gt;Description&lt;/label&gt; &lt;input type="text" id="description" name="description"&gt; &lt;/p&gt; &lt;input type="submit" name="submit" value="Submit"&gt; &lt;/form&gt; &lt;ul&gt; {% for i in imgs %} &lt;li&gt; &lt;img src="{{i.img_url}}=s400-c" alt=""&gt; {{i.description }} &lt;/li&gt; &lt;li&gt;{{i.when }}&lt;/li&gt; &lt;li&gt; &lt;form action="/delete" method="POST" enctype="multipart/form-data"&gt; &lt;input type="text" name="k" value="{{i.key}}" /&gt; &lt;input type="submit" value="delete"&gt; &lt;/form&gt; &lt;/li&gt; {% endfor %} &lt;/ul&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload