-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
32 lines (26 loc) · 1006 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from os.path import abspath, dirname
from flask import Flask, request, render_template, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import TextField
app = Flask(__name__, template_folder="templates")
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
app.config['SECRET_KEY'] = "secret"
db = SQLAlchemy(app)
#app.root_path = abspath(dirname(__file__))
class TextForm(FlaskForm):
content = TextField("Content")
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(80), nullable=False)
def __repr__(self):
return 'Post Content: %r' % self.text
@app.route('/', methods = ['GET', 'POST'])
def main():
form = TextForm(request.form)
if request.method == 'POST':
post = Post(text=form.content.data)
db.session.add(post)
db.session.commit()
return redirect("/")
return render_template('main.html', form=form, results=Post.query.all())