From 315f926700b1d1be79f342a1b606a284c0eb8499 Mon Sep 17 00:00:00 2001 From: igor Date: Tue, 27 Jan 2026 09:26:01 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=82=D1=80=D0=BE=D0=B9=D1=82?= =?UTF-8?q?=D0=B5=20=D0=B1=D0=B0=D0=B7=D1=83=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85.=20=D0=9D=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9?= =?UTF-8?q?=D1=82=D0=B5=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D1=81=D1=82=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=B1=D0=BB=D0=BB=D0=BE=D0=B3=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 26 +++++++++++++++++++++++++- database.db | Bin 0 -> 12288 bytes init_db.py | 20 ++++++++++++++++++++ schema.sql | 8 ++++++++ templates/index.html | 7 +++++++ templates/post.html | 7 +++++++ 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 database.db create mode 100644 init_db.py create mode 100644 schema.sql create mode 100644 templates/post.html diff --git a/app.py b/app.py index 0a6c573..3032501 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,32 @@ +import sqlite3 from flask import Flask, render_template +from werkzeug.exceptions import abort + +def get_db_connection(): + conn = sqlite3.connect('database.db') + conn.row_factory = sqlite3.Row + return conn + +def get_post(post_id): + conn = get_db_connection() + post = conn.execute('SELECT * FROM posts WHERE id = ?', + (post_id,)).fetchone() + conn.close() + if post is None: + abort(404) + return post app = Flask(__name__) @app.route('/') def index(): - return render_template('index.html') + conn = get_db_connection() + posts = conn.execute('SELECT * FROM posts').fetchall() + conn.close() + return render_template('index.html', posts=posts) + +@app.route('/') +def post(post_id): + post = get_post(post_id) + return render_template('post.html', post=post) diff --git a/database.db b/database.db new file mode 100644 index 0000000000000000000000000000000000000000..622742a38907874d7fcdc7d5a2f48d640677f704 GIT binary patch literal 12288 zcmeI&%}&BV6b0a!;!iYD+;w}68x3d>5{AByb8gychqW z|5lFJdU#&qE@$~wr1yA0K>z{}fB*y_009U<00Izz00jO{pz6%InT%}M)>Y5M!yfN> zo3R<*o2dPLnkt6M(~7ir>?``#yB@XrCdNuGH(h;5t<&X;`h6Rj^2 zSGPmuK&w*~QZ+1Bys$xMszF{|pO*tpRaBs}NoRXxTGn((mn*8Ky-JmWb4@|r_vu8H zyt=QcSPw()Z+^CXByHFB3~6=A?UTslk%P#Z$bMZ~+(`E3=_iqX;Q<8!2tWV=5P$## zAOHafKmY;|fWRLK96Gb2uvX0H@;jTk?ally<@WZs`N-Eydk}S~%C8oRlifRirzd-B zDE?R)zg;+xoL}{R$d|gK*!~#5PFBY0@%~??-vl2h2tWV=5P$##AOHafKmY;|fB*#k OlfZ&Zxia}@g7Xd>?uk|a literal 0 HcmV?d00001 diff --git a/init_db.py b/init_db.py new file mode 100644 index 0000000..b2afd00 --- /dev/null +++ b/init_db.py @@ -0,0 +1,20 @@ +import sqlite3 + +connection = sqlite3.connect('database.db') + + +with open('schema.sql') as f: + connection.executescript(f.read()) + +cur = connection.cursor() + +cur.execute("INSERT INTO posts (title, content) VALUES (?, ?)", + ('First Post', 'Content for the first post') + ) + +cur.execute("INSERT INTO posts (title, content) VALUES (?, ?)", + ('Second Post', 'Content for the second post') + ) + +connection.commit() +connection.close() diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..1f1ce54 --- /dev/null +++ b/schema.sql @@ -0,0 +1,8 @@ +DROP TABLE IF EXISTS posts; + +CREATE TABLE posts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + title TEXT NOT NULL, + content TEXT NOT NULL +); diff --git a/templates/index.html b/templates/index.html index 8553659..08769cf 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,5 +2,12 @@ {% block content %}

{% block title %} Welcome to FlaskBlog {% endblock %}

+ {% for post in posts %} + +

{{ post['title'] }}

+
+ {{ post['created'] }} +
+ {% endfor %} {% endblock %} diff --git a/templates/post.html b/templates/post.html new file mode 100644 index 0000000..fea2712 --- /dev/null +++ b/templates/post.html @@ -0,0 +1,7 @@ +{% extends 'base.html' %} + +{% block content %} +

{% block title %} {{ post['title'] }} {% endblock %}

+

{{ post['content'] }}

+ {{ post['created'] }} +{% endblock %}