from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
def init_db():
with sqlite3.connect(“comments.db”) as conn:
conn.execute(“CREATE TABLE IF NOT EXISTS comments (id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT)”)
@app.route(‘/’)
def home():
return ”’
Comentarios Anónimos
Deja tu comentario anónimo
”’
@app.route(‘/submit’, methods=[‘POST’])
def submit():
comment = request.form.get(“comment”)
if comment:
with sqlite3.connect(“comments.db”) as conn:
conn.execute(“INSERT INTO comments (text) VALUES (?)”, (comment,))
return “Comentario enviado. Gracias!”
@app.route(‘/admin’)
def admin():
with sqlite3.connect(“comments.db”) as conn:
comments = conn.execute(“SELECT * FROM comments”).fetchall()
return render_template(“admin.html”, comments=comments)
if __name__ == ‘__main__’:
init_db()
app.run(debug=True)