-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathapp.py
39 lines (27 loc) · 953 Bytes
/
app.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
33
34
35
36
37
38
39
import os
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
DATABASE_URL = os.environ.get('DATABASE_URL', 'sqlite:////tmp/flask_app.db')
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
email = db.Column(db.String(100))
def __init__(self, name, email):
self.name = name
self.email = email
@app.route('/', methods=['GET'])
def index():
return render_template('index.html', users=User.query.all())
@app.route('/user', methods=['POST'])
def user():
u = User(request.form['name'], request.form['email'])
db.session.add(u)
db.session.commit()
return redirect(url_for('index'))
if __name__ == '__main__':
db.create_all()
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)