上下文:我正在创建我的第一个Flask Web应用程序作为Twitter克隆,开发人员可以在其中将“开发日志”发布到他们正在开发的游戏中。
从本教程中学到的知识,我将应用程序重新打包为四个python模块。我仍然有些困惑,我认为我做错了一些事情,但是当我通过以下脚本运行程序时:
from home import app
if __name__ == "__main__":
app.run(debug=True)
我的烧瓶应用的名称在哪里。运行此命令将输出此错误:
Traceback (most recent call last):
File "run.py", line 1, in <module>
from home import app
File "/Users/AdityaD/iCloud Drive (Archive)/Desktop/DevlogIO/home/__init__.py", line 11, in <module>
from home import routes
File "/Users/AdityaD/iCloud Drive (Archive)/Desktop/DevlogIO/home/routes.py", line 10, in <module>
db.session.commit()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 153, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 943, in commit
self.transaction.commit()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 467, in commit
self._prepare_impl()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 447, in _prepare_impl
self.session.flush()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2254, in flush
self._flush(objects)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2381, in _flush
transaction.rollback(_capture_exception=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 249, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2345, in _flush
flush_context.execute()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 395, in execute
rec.execute(self)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 560, in execute
uow
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 181, in save_obj
mapper, table, insert)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 866, in _emit_insert_statements
execute(statement, params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 948, in execute
return meth(self, multiparams, params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
compiled_sql, distilled_params
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
context)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
exc_info
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 248, in reraise
raise value.with_traceback(tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
context)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user [SQL: 'INSERT INTO user (username, password) VALUES (?, ?)'] [parameters: ('admin', 'admin123')] (Background on this error at: http://sqlalche.me/e/e3q8)
这是我的初始化文件:
from flask import Flask
from flask_session import Session
# Set's up the app with the given name
app = Flask(__name__)
# Secret key for sessions
app.config['SECRET_KEY'] = 'eb02cfb5079a2b6bdeb8bddb69ca937b'
# Sets up config for database with SQLite, which is easy to set up.
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
from home import routes
from home.models import *
db.create_all()
下面是routes.py:
from flask import render_template, url_for, flash
import os, datetime
from home import app
from home.models import db
#dummy data
import home.models
theUser = home.models.User(username='admin', password='admin123')
db.session.add(theUser)
db.session.commit()
admin = home.models.User.query.filter_by(username='admin').first()
zombies = home.models.Games(game='Shoot Zombies!', user_id=admin.id)
ball = home.models.Games(game='Bounce & Ball', user_id=admin.id)
db.session.add(zombies)
db.session.add(ball)
db.session.commit()
def getCurrentUser():
return admin
from home import posts
@app.route("/", methods=['GET', 'POST'])
def index():
form = posts.WriteLog()
if form.validate_on_submit():
username = current_user.username
post = form.devLog.data
game = Games.query.filter_by(game=form.gameSelect.data).first().id
title = form.title.date
newPost = Post(title=title, game_id=game, content=post)
return render_template("index.html", form=form, data=Post)
return render_template("index.html", form=form, data=Post)
下面是models.py:
from home import app
from flask_sqlalchemy import SQLAlchemy
import os, datetime
# Sets up SQL Database
db = SQLAlchemy(app)
### Database Stuff ###
# Creates a table for the user
class User(db.Model):
__tablename__ = "user"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False)
password = db.Column(db.String(20), nullable=False)
games = db.relationship('Games', backref="user", lazy=True)
def __repr__(self):
return f"User('{self.username}', '{self.password}', '{self.games}')"
# Creates a table for games
class Games(db.Model):
__tablename__ = "game"
id = db.Column(db.Integer, primary_key=True)
game = db.Column(db.String(40), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
logs = db.relationship('Post', backref="game", lazy=True)
def __repr__(self):
return f"Game('{self.game}', '{self.logs}')"
class Post(db.Model):
__tablename__ = "post"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False)
content = db.Column(db.Text, nullable=False)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}', '{self.game_id}', '{self.content}')"
posts.py只是一个管理表单的应用程序。据我了解,未创建User表。但是,我已经在init文件中运行了db.create_all()命令。帮助将不胜感激!
更新:现在我有一个新错误。
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 140734986048960 and this is thread id 123145577107456 [SQL: 'SELECT post.id AS post_id, post.title AS post_title, post.date_posted AS post_date_posted, post.game_id AS post_game_id, post.content AS post_content \nFROM post \nWHERE ? = post.game_id'] [parameters: [{'%(4536059552 param)s': 1}]] (Background on this error at: http://sqlalche.me/e/f405)
作为参考,这是我的index.html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>DevlogIO</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div id="container">
<h1>Devlog IO</h1>
<h2>Home</h2>
<p>See the progress of your favorite developers, and discover new and rising game developers!</p>
<div>
<form action="" method="POST">
{{ form.hidden_tag() }}
<fieldset>
<legend>What was your progress for today?</legend>
<div>
{{ form.gameSelect(class="") }}
</div>
<br>
<div>
{{ form.title.label(class="") }}
{{ form.title(class="") }}
</div>
<br>
<div>
{{ form.devLog.label(class="") }}
{{ form.devLog(class="") }}
</div>
<br>
<div>
{{ form.submit(class="") }}
</div>
<br>
</fieldset>
</form>
</div>
<hr>
<div id="stream">
{% for post in data.query.all() %}
<fieldset>
<p><strong>{{ post.title }}, {{ form.gameSelect.data }}</strong></p>
<p><i>by {{ post.game_id }}</i></p>
<p><i>{{ post.date_posted }}</i></p>
<p>{{ post.content }}</p>
</fieldset>
<br>
{% endfor %}
</div>
</div>
<script src="{{ url_for('static', filename='script.js') }}"></script>
</body>
</html>
答案 0 :(得分:0)
表示错误出现在第11行:
from home import routes
您的应用甚至没有进入db.create_all()
,并且在导入时失败,在该导入中,您对尚不存在的db对象的引用还存在。
尝试将db.create_all()
放入route.py