Going off the documentation here.
I have this code:
from playhouse.flask_utils import FlaskDB,
app = Flask(__name__)
app.config.from_object(__name__)
flask_db = FlaskDB(app)
database = flask_db.database
class Item(flask_db.Model):
title = CharField()
content = TextField()
category = CharField()
@app.route('/create',methods=('GET','POST'))
def create():
if request.method == 'POST':
if request.form.get('title') and request.form.get('content'):
item = Item.create(
title = request.form['title'],
content = request.form['content'],
category = request.form['category'])
flash('Item created successfully','success')
return redirect(url_for('view'),item=item)
else:
flash('Title and Content are required.','danger')
form = ItemForm()
return render_template('create.html',form=form)
if __name__ == '__main__':
database.create_tables(Item)
app.run(debug=True)
They say it:
Dynamically create a Peewee database instance based on app config data.
However, I believe I still need to create the tables, in fact when I tried it without doing that second to last line, I could see that no tables existed in the created blog.db file. Unfortunately when I run this now, I get:
Traceback (most recent call last):
sqliteext:////Users/conduce-laptop/PycharmProjects/alexmarshall.website/blog.db
File "/Users/conduce-laptop/PycharmProjects/alexmarshall.website/website2.py", line 73, in <module>
database.create_tables(Item)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3855, in create_tables
create_model_tables(models, fail_silently=safe)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 5293, in create_model_tables
for m in sort_models_topologically(models):
File "playhouse/_speedups.pyx", line 341, in playhouse._speedups.sort_models_topologically (playhouse/_speedups.c:7091)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 4862, in __iter__
return iter(self.select())
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3240, in __iter__
return iter(self.execute())
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3233, in execute
self._qr = ResultWrapper(model_class, self._execute(), query_meta)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 2912, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3775, in execute_sql
self.commit()
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3598, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3768, in execute_sql
cursor.execute(sql, params or ())
peewee.OperationalError: no such table: item
You were invoking the create_tables incorrectly:
database.create_tables([Item], True)
The True
allows you to call it multiple times without errors.