我不明白为什么我要在BadRequestKeyError: 400 Bad Request: KeyError: 'todoitem'
路线上到达delete
。
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///my_todo.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
text= db.Column(db.String(200))
complete= db.Column(db.Boolean)
@app.route('/delete/<int:id>')
def delete(id):
todo = Todo(text=request.form['todoitem'], complete=False)
todo = todo.query.filter_by(_id = id).first()
db.session.delete(todo)
db.session.commit()
incomplete = Todo.query.filter_by(complete=False).all()
complete = Todo.query.filter_by(complete=True).all()
return render_template('index.html', incomplete=incomplete, complete=complete)
<ul>
{% for todo in complete %}
<li>{{todo.text}}</li><a href="{{url_for('delete', id=todo.id)}}"> Deletar</a>
{% endfor %}
</ul>