我已经从 flask-mongokit 示例中修改了 list.html 模板。
添加了名称为“ck”的“复选框”和一个删除链接。
{% extends "base.html" %}
{% block body %}
<h2>All Items</h2>
<ul>
<form id="my_form" action="delete" method=get>
{% for task in tasks %}
<li><input type="checkbox" name="ck" value="{{ task._id }}"><a href="{{ url_for('show_task', task_id=task._id) }}" >{{ task.title }}</a> - Created: {{ task.creation.strftime('%Y-%m-%d %H:%M') }}</li>
{% endfor %}
</ul>
<a href="{{ url_for('new_task') }}">Add New Task</a> <br>
<a href="javascript:{}" onclick="document.getElementById('my_form').submit(); return false;">Delete Selected Task</a>
</form>
{% endblock %}
--------------------------------
from datetime import datetime
from flask import Flask, request, render_template, redirect, url_for
from flask.ext.mongokit import MongoKit, Document
import bson
app = Flask(__name__)
class Task(Document):
__collection__ = 'tasks'
structure = {
'title': unicode,
'text': unicode,
'creation': datetime,
}
required_fields = ['title', 'creation']
default_values = {'creation': datetime.utcnow()}
use_dot_notation = True
db = MongoKit(app)
db.register([Task])
@app.route('/delete', methods=["GET", "POST"])
def task_delete():
if request.method == 'GET':
order = request.args.getlist('ck')
for id in order:
db.Task.remove({'_id':bson.ObjectId(oid=str(id))})
return redirect(url_for('show_all'))
以下是错误信息:
File "C:\Users\krisk\My Documents\Aptana Studio 3 Workspace\flask-mongokit\example\todo.py", line 58, in task_delete
db.Task.remove({'_id':bson.ObjectId(oid=str(id))})
File "C:\Python27\lib\site-packages\mongokit\schema_document.py", line 379, in __getattr__
return dict.__getattribute__(self, key)
**AttributeError: 'CallableTask' object has no attribute 'remove'**
我不确定为什么 Collections.Remove() 方法不起作用。