如何处理Google App Engine中的删除(Python)(How to handle delete in Google App Engine (Python))
我是一位新手程序员,也是Google App Engine和webapp2等的新手。所以这可能是一个非常基本的问题。
我正在创建一个应用程序来将图像存储到BlobStore中。 我的模型存储说明,blob_key,图片网址和日期。
我能够拯救一切,所以这一点是好的。
但是现在我想要创建一个删除按钮,它不仅会删除数据存储中的项目,还会删除保存在Blobstore中的图像。
我创建了一个DeleteHandler,并且在html中我有一个表单,为要删除的项目传递密钥。 在DeleteHandler中,我使用已发布的密钥从数据存储中删除项目。 我也尝试使用该键来删除保存在Blobstore中的图像。
到目前为止,我在删除表单文章中获得了404,即使我已经过去了,我也不确定我的DeleteHandler是否正确处理我正在寻找的功能。
任何帮助将非常感激..
Main.py:
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)
main.html中:
<form action="{{upload_url}}" method="POST" enctype="multipart/form-data"> <p> <label for="file">Upload File</label> <input type="file" name="file" id="file"> </p> <p> <label for="description">Description</label> <input type="text" id="description" name="description"> </p> <input type="submit" name="submit" value="Submit"> </form> <ul> {% for i in imgs %} <li> <img src="{{i.img_url}}=s400-c" alt=""> {{i.description }} </li> <li>{{i.when }}</li> <li> <form action="/delete" method="POST" enctype="multipart/form-data"> <input type="text" name="k" value="{{i.key}}" /> <input type="submit" value="delete"> </form> </li> {% endfor %} </ul>
I'm a newbie programmer and new to Google App Engine and webapp2 etc. So this may be a very basic question.
I am creating an application to store images into BlobStore. My model stores description, blob_key, image url and date.
I am able to save everything, so that bit is okay.
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.
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.
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.
Any help would be much appreciated..
Main.py:
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)
Main.html:
<form action="{{upload_url}}" method="POST" enctype="multipart/form-data"> <p> <label for="file">Upload File</label> <input type="file" name="file" id="file"> </p> <p> <label for="description">Description</label> <input type="text" id="description" name="description"> </p> <input type="submit" name="submit" value="Submit"> </form> <ul> {% for i in imgs %} <li> <img src="{{i.img_url}}=s400-c" alt=""> {{i.description }} </li> <li>{{i.when }}</li> <li> <form action="/delete" method="POST" enctype="multipart/form-data"> <input type="text" name="k" value="{{i.key}}" /> <input type="submit" value="delete"> </form> </li> {% endfor %} </ul>
原文:https://stackoverflow.com/questions/13432054
最满意答案
您可以在ListView构造函数中指定RecycleElement,而不是使用x:Arguments
<ListView ...> <x:Arguments> <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy> </x:Arguments> </ListView>
You can specify RecycleElement in the ListView constructor instead using x:Arguments
<ListView ...> <x:Arguments> <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy> </x:Arguments> </ListView>
相关问答
更多-
您可以在ListView构造函数中指定RecycleElement,而不是使用x:Arguments
RecycleElement -
创建一个模型MainUIModel public class MainUIModel : INotifyPropertyChanged { ObservableCollection
_randomList; public ObservableCollection randomList { get { return _randomList; } set { if (_ ... -
是的! 您需要在ListView HasUnevenRows设置为true。 然后将标题模板设置为ViewCell ,其高度为0,带有空网格或其他元素。
如何根据listview xamarin表单中的类别对列表进行分组(How to group the list based on category in listview xamarin forms)[2022-05-09]
你可以在这里找到好的XF样本 You can find good XF sample here尝试这个:AQuery库解决了我的问题。 希望它也适用于其他人。 AQuery library have solved my issue. Hope it will work for someone else as well.最可能的问题是您声明FilteredContacts的方式,它应该是一个属性,而不是一个字段。 不包括用于绑定搜索属性的反射机制。 Most likely the problem is with the way you declared FilteredContacts, it should be a property, and not a field. The reflection mechanism used in binding only searches for properties, fields ...我该如何解决? 这样的列表是显示下载文件的好方法吗? 不知道为什么,当你开始一个像你在代码中那样继承自ListActivity的新活动时,我没有任何信息的异常,所以我决定在其布局中使用普通的Activity和ListView控件,例如: downloadslayout.axml:您是否尝试过设置ListView的HasUnevenRows属性? https://developer.xamarin.com/api/property/Xamarin.Forms.ListView.HasUnevenRows/Have you tried to set the HasUnevenRows property of the ListView? https://developer.xamarin.com/api/propert ... Xamarin.Forms在ListView中显示文本而不是typename(Xamarin.Forms Display Text in ListView instead of typename)[2022-04-15]
试试这种方式: public class LCIDEditor : ContentPage { private ListView _lv; private ObservableCollection相关文章
更多- pychseg - A Python Chinese Segment Project - Google Project Hosting
- 最简单下拉刷新,Google最新(可刷新任何控件)
- python top project of 2013
- 十分钟了解分布式计算:Google Dataflow
- 全球最流行的66款App的共同规律
- Twitter Storm Real-Life App 排错记
- 开发android App干坏事(一)
- 【转】中国正爆发聊天APP大战 未来或影响西方
- 手机app
- Python资源索引 【转载】
最新问答
更多- 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
- 如何通过引用返回对象?(How is returning an object by reference possible?)
- 矩阵如何存储在内存中?(How are matrices stored in memory?)
- 每个请求的Java新会话?(Java New Session For Each Request?)
- css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
- 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
- xcode语法颜色编码解释?(xcode syntax color coding explained?)
- 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
- 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
- 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
- 西安哪有PLC可控制编程的培训
- 在Entity Framework中选择基类(Select base class in Entity Framework)
- 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
- 电脑二级VF有什么用
- Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
- 金华英语角.
- 手机软件如何制作
- 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
- 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
- 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
- Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
- 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
- python的访问器方法有哪些
- DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
- 在Ruby中对组合进行排序(Sorting a combination in Ruby)
- 网站开发的流程?
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。