Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. from flask import Flask, redirect, render_template, request, url_for
  2. from flask.ext.sqlalchemy import SQLAlchemy
  3.  
  4. app = Flask(__name__)
  5. app.config["DEBUG"] = True
  6.  
  7. SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
  8. username="your-own-username",
  9. password="your-own-password",
  10. hostname="dhsstudent1.mysql.pythonanywhere-services.com",
  11. databasename="dhsstudent1$comments",
  12. )
  13. app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
  14. app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
  15.  
  16. db = SQLAlchemy(app)
  17.  
  18. class Comment(db.Model):
  19.  
  20. __tablename__ = "comments"
  21.  
  22. id = db.Column(db.Integer, primary_key=True)
  23. content = db.Column(db.String(4096))
  24.  
  25. @app.route("/", methods=["GET", "POST"])
  26. def index():
  27. if request.method == "GET":
  28. return render_template("main_page.html", comments=Comment.query.all())
  29.  
  30. comment = Comment(content=request.form["comment"])
  31. db.session.add(comment)
  32. db.session.commit()
  33.  
  34. return redirect(url_for('index'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement