From 8389853e04417869c8d9833e6000ce6c6452dc8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Sat, 5 Apr 2025 09:09:38 +0200 Subject: [PATCH] =?UTF-8?q?mini=20Programm=20l=C3=A4uft.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 58 ++++++++++++++++++++++++++++++++++++++++--- buecher.sql | Bin 0 -> 8192 bytes templates/index.html | 13 +++++++++- 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 buecher.sql diff --git a/app.py b/app.py index 57c26a7..71503ed 100644 --- a/app.py +++ b/app.py @@ -1,10 +1,62 @@ -from flask import Flask +from flask import Flask, render_template, request, redirect, url_for +from flask_sqlalchemy import SQLAlchemy +import os + +basedir = os.path.abspath(os.path.dirname(__file__)) app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'buecher.sql') +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +db = SQLAlchemy(app) +# Datenbankmodell für ein Buch +class Buch(db.Model): + id = db.Column(db.Integer, primary_key=True) + titel = db.Column(db.String(80), nullable=False) + autor = db.Column(db.String(80), nullable=False) + jahr = db.Column(db.Integer) + + def __repr__(self): + return f'' + +with app.app_context(): + db.create_all() + +# Routen für die Anwendung @app.route('/') -def hello_world(): - return 'Hello, World!' +def index(): + buecher = Buch.query.all() + return render_template('index.html', buecher=buecher) + +@app.route('/hinzufuegen', methods=['GET', 'POST']) +def hinzufuegen(): + if request.method == 'POST': + titel = request.form['titel'] + autor = request.form['autor'] + jahr = request.form.get('jahr', None) # Jahr kann optional sein + neues_buch = Buch(titel=titel, autor=autor, jahr=jahr) + db.session.add(neues_buch) + db.session.commit() + return redirect(url_for('index')) + return render_template('hinzufuegen.html') + +@app.route('/bearbeiten/', methods=['GET', 'POST']) +def bearbeiten(id): + buch = Buch.query.get_or_404(id) + if request.method == 'POST': + buch.titel = request.form['titel'] + buch.autor = request.form['autor'] + buch.jahr = request.form.get('jahr', None) + db.session.commit() + return redirect(url_for('index')) + return render_template('bearbeiten.html', buch=buch) + +@app.route('/loeschen/') +def loeschen(id): + buch = Buch.query.get_or_404(id) + db.session.delete(buch) + db.session.commit() + return redirect(url_for('index')) if __name__ == '__main__': app.run(debug=True) diff --git a/buecher.sql b/buecher.sql new file mode 100644 index 0000000000000000000000000000000000000000..c1a0873c95272fbcc93f697db8ffbcb4d78facc1 GIT binary patch literal 8192 zcmeI#K??yv6bJAbDcKSiIeKopB`Ft2Lld$VV<{K4yU2>vHa-BK+{|GQ4szH3HLvN- zo8IAfd+U{->LZ=**)7u4V>QM(8xS$ZxXFUaJZxRe3v=80S8&!zc*XqU)jcx@0Rad= z00Izz00bZa0SG_<0ucB^foGGK8udDVwR99;kMScp+boQf6rrS041G!Vg28OFsev5P$##AOHafKmY;|fB*y_0D(Uicmsb{G0y-1 literal 0 HcmV?d00001 diff --git a/templates/index.html b/templates/index.html index d688f85..b1edcd6 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,10 +3,21 @@ - Meine Bücherverwaltung + BibelTresor +

Meine Bücher

+
    + {% for buch in buecher %} +
  • {{ buch.titel }} von {{ buch.autor }} ({{ buch.jahr }}) - + Bearbeiten - + Löschen +
  • + {% endfor %} +
+

Neues Buch hinzufügen

+