Created
April 17, 2019 13:41
-
-
Save tegarimansyah/503ae0d56d7d44525ef9565d56245c0c to your computer and use it in GitHub Desktop.
Belajar Flask + SQLAlchemy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
from flask_sqlalchemy import SQLAlchemy | |
# http://flask-sqlalchemy.pocoo.org/2.3/quickstart/#a-minimal-application | |
app = Flask(__name__) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://my_user:my_password@127.0.0.1/my_database' | |
db = SQLAlchemy(app) | |
class User(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
username = db.Column(db.String(80), unique=True, nullable=False) | |
email = db.Column(db.String(120), unique=True, nullable=False) | |
def __repr__(self): | |
return '<User %r>' % self.username | |
def __init__(self, username, email): | |
self.username = username | |
self.email = email | |
db.session.add(self) | |
db.session.commit() | |
@app.route("/") | |
def hello(): | |
return "Hello World!" | |
@app.route("/register", methods=["POST"]) | |
def hello2(): | |
content = request.json | |
print(content['username']) | |
print(content['email']) | |
new_user = User(content['username'], content['email']) | |
return jsonify({"data":content}) | |
if __name__ == "__main__": | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment