import sqlite3 from contextlib import closing from flask import * DEBUG=True PORT=80 ANDMEBAAS="postitusbaas.db" app=Flask(__name__) app.config.from_object(__name__) #suurtähtedega sõnad konfiks def yhendu_db(): return sqlite3.connect(app.config["ANDMEBAAS"]) #Eraldi käsurealt käima #import postitused1 #postitused1.loo_db() def loo_db(): with closing(yhendu_db()) as db: with app.open_resource('postitustabel.sql') as f: db.cursor().executescript(f.read()) db.commit() @app.before_request def before_request(): g.db=yhendu_db() @app.teardown_request def teardown_request(ex): if hasattr(g, "db"): g.db.close() @app.route('/') def avaleht(): return render_template("juht.html") @app.route('/vaata') def vaata(): andmeparing=g.db.execute("select postitaja, postitus from postitused") postitused=[dict(postitaja=rida[0], postitus=rida[1]) for rida in andmeparing.fetchall()] return render_template('postitused.html', postitused=postitused) @app.route('/saada', methods=['GET', 'POST']) def saada(): if request.method=="POST": autor=request.form['autor'] postitus=request.form['postitus'] g.db.execute('insert into postitused (postitaja, postitus) values (?, ?);', [autor, postitus]) g.db.commit() return "valmis" else: return render_template('postita.html') if __name__=="__main__": app.run()