【发布时间】:2019-08-29 06:56:38
【问题描述】:
我正在 youtube 上做一个烧瓶教程。但是我总是得到这个错误。我什至复制了他的代码并在我的电脑上尝试过,但它仍然会引发该错误。
from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"
db = SQLAlchemy(app)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return "<Task %r>" % self.id
@app.route("/", methods=["POST", "GET"])
def index():
if request.method == "POST":
task_content = request.form["content"]
new_task = Todo(content=task_content)
try:
db.session.add(new_task)
db.session.commit()
return redirect("/")
except:
return "There was an issue adding your task"
else:
tasks = Todo.query.order_by(Todo.date_created).all()
return render_template("index.html", tasks=tasks)
# return "hello"
if __name__ == "__main__":
app.run(debug=True)
这是错误:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的表:todo [SQL: SELECT todo.id AS todo_id, todo.content AS todo_content, todo.date_created AS todo_date_created FROM todo ORDER BY todo.date_created]
【问题讨论】: