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,
Secret_key='This must been secret !',
SQLALCHEMY_DATABASE_URI='sqlite:///app_db.db',
SQLALCHEMY_TRACK_MODIFICATIONS=False,
WTF_CSRF_ENABLE=False,
)
db = SQLAlchemy(app)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True, index=True)
post_name = db.Column(db.String(75), nullable=False)
post_body = db.Column(db.String(3000), nullable=False)
class Comments(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True, index=True)
post_id = db.Column(
db.Integer,
db.ForeignKey('post.id'),
index=True,
nullable=False,
)
post = db.relationship('Post', foreign_keys=[post_id, ])
post_comment = db.Column(db.String(1500), nullable=False)
def __str__(self):
return '<Comments %r>' % self.data
class PostForm(ModelForm):
class Meta:
model = Post()
# Some wrong here !!!!!!!!!!!!!!!!!!
@app.route('/')
def index():
return Post.querry.all()
# And here !!!!!!!!!!!!!!!!!!!!!
@app.route('/add_post', methods=['POST'])
def add_post():
form = PostForm(request.form)
if form.validate():
post = Post(**form.data)
db.session.add(post)
db.session.commit()
return ('Post added !', 200)
else:
return ('Bad input', 200)
if __name__ == '__main__':
db.create_all()
app.run()