Answer the question
In order to leave comments, you need to log in
How to pull data from sqlite/sqlalchemy?
I wrote a simple flask application that writes something to the database using sqlalchemy, how can I make it so that I can get the entire table from the database?
from datetime import date
from flask import Flask, request, flash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.testing import db
from wtforms_alchemy import ModelForm
app = Flask(__name__)
app.config.update(
DEBUG=True,
SECURE_KEY='This must be secret !',
SQLALCHEMY_DATABASE_URI='sqlite:///test.db',
SQLALCHEMY_TRACK_NOTIFICATION=False,
WTF_CSRF_ENABLE=False
)
db = SQLAlchemy(app)
class GuessBookItem(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
author = db.Column(db.String(5, ))
date_create = db.Column(db.Date, default=date.today())
is_visible = db.Column(db.Boolean, default=True, nullable=False)
def to_dict(self):
return {
'id': self.id,
'author': self.author,
'data_create': self.date_create,
'is_visible': self.is_visible
}
class GuessBookForm(ModelForm):
class Meta:
model = GuessBookItem
# Function must to return table
@app.route('/', methods=['GET'])
def main():
pass
@app.route('/create')
def create():
print(request.form)
form = GuessBookForm(request.form)
if form.validate():
guess_book = GuessBookItem(**form.data)
db.session.add(guess_book)
db.session.commit()
flash('DataBase update')
if __name__ == '__main__':
db.create_all()
GuessBookItem.query.delete()
ivan = GuessBookItem(author='Ivan')
db.session.add(ivan)
db.session.commit()
app.run()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question