[Python 100μΌ μ±λ¦°μ§] Day 90 - π― λ―Έλ νλ‘μ νΈ: λΈλ‘κ·Έ μΉμ¬μ΄νΈ
π Phase 9μ λ§μ§λ§ λ μ λλ€!
μ§λ 10μΌκ° Flaskλ‘ μΉ κ°λ°μ ν΅μ¬μ λͺ¨λ λ°°μ μ΅λλ€:
- Day 81: Flask μμνκΈ°
- Day 82: λΌμ°ν κ³Ό URL μ²λ¦¬
- Day 83: ν νλ¦Ώ μμ§ (Jinja2)
- Day 84: νΌ μ²λ¦¬
- Day 85: λ°μ΄ν°λ² μ΄μ€ μ°λ
- Day 86: CRUD μ ν리μΌμ΄μ
- Day 87: REST API
- Day 88: μ¬μ©μ μΈμ¦
- Day 89: λ°°ν¬ μ€λΉ
μ€λμ μ΄ λͺ¨λ κ²μ νλλ‘ ν©μΉ©λλ€!
μμ ν κΈ°λ₯μ κ°μ§ λΈλ‘κ·Έ μΉμ¬μ΄νΈλ₯Ό λ§λ€μ΄λ΄ μλ€: β νμκ°μ /λ‘κ·ΈμΈ β κΈ μμ±/μμ /μμ β λκΈ μμ€ν β κ²μ κΈ°λ₯ β REST API β κ΄λ¦¬μ κΈ°λ₯
μ€μ μμ λ°λ‘ μ¬μ©ν μ μλ μμ±λ λμ νλ‘μ νΈμ λλ€! πͺ
(60λΆ μλ βββββ)
π― νλ‘μ νΈ κ°μ
κΈ°λ₯ λͺ μΈ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# π€ μ¬μ©μ κΈ°λ₯
- νμκ°μ
(μ΄λ©μΌ μΈμ¦ μ μΈ)
- λ‘κ·ΈμΈ/λ‘κ·Έμμ
- νλ‘ν μμ
- λΉλ°λ²νΈ λ³κ²½
# π λΈλ‘κ·Έ ν¬μ€νΈ
- κΈ μμ± (μ λͺ©, λ΄μ©, μΉ΄ν
κ³ λ¦¬)
- κΈ μμ (μμ±μλ§)
- κΈ μμ (μμ±μ/κ΄λ¦¬μλ§)
- κΈ λͺ©λ‘ (νμ΄μ§λ€μ΄μ
)
- κΈ μμΈ λ³΄κΈ°
# π¬ λκΈ
- λκΈ μμ± (λ‘κ·ΈμΈ νμ)
- λκΈ μμ (μμ±μ/κ΄λ¦¬μλ§)
# π κ²μ
- μ λͺ©/λ΄μ©μΌλ‘ κ²μ
- μΉ΄ν
κ³ λ¦¬λ³ νν°λ§
# π§ κ΄λ¦¬μ
- λͺ¨λ κΈ/λκΈ κ΄λ¦¬
- μ¬μ©μ κ΄λ¦¬
# π REST API
- GET /api/posts - μ 체 κΈ
- GET /api/posts/<id> - νΉμ κΈ
- POST /api/posts - κΈ μμ± (μΈμ¦ νμ)
νλ‘μ νΈ κ΅¬μ‘°
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
blog-website/
βββ app.py # λ©μΈ μ ν리μΌμ΄μ
βββ config.py # μ€μ νμΌ
βββ init_db.py # DB μ΄κΈ°ν
βββ models.py # λ°μ΄ν° λͺ¨λΈ (μ ν)
βββ requirements.txt # ν¨ν€μ§ λͺ©λ‘
βββ .env # νκ²½ λ³μ
βββ database.db # SQLite DB
βββ templates/
β βββ base.html # λ² μ΄μ€ ν
νλ¦Ώ
β βββ index.html # λ©μΈ νμ΄μ§
β βββ auth/
β β βββ register.html
β β βββ login.html
β βββ posts/
β β βββ list.html
β β βββ detail.html
β β βββ create.html
β β βββ edit.html
β βββ user/
β βββ profile.html
βββ static/
βββ css/
β βββ style.css
βββ js/
βββ main.js
π¦ 1λ¨κ³: νλ‘μ νΈ μ΄κΈ° μ€μ
1. κ°μ νκ²½ μμ±
1
2
3
4
5
6
7
8
9
10
11
12
# νλ‘μ νΈ ν΄λ μμ±
mkdir blog-website
cd blog-website
# κ°μ νκ²½ μμ±
python -m venv venv
# νμ±ν
# Windows
venv\Scripts\activate
# Mac/Linux
source venv/bin/activate
2. ν¨ν€μ§ μ€μΉ
1
pip install flask python-dotenv bcrypt
requirements.txt:
1
2
3
Flask==3.0.0
python-dotenv==1.0.0
bcrypt==4.1.1
3. νκ²½ λ³μ μ€μ
.env:
1
2
3
SECRET_KEY=your-secret-key-change-this-in-production
DATABASE_URI=sqlite:///database.db
DEBUG=True
4. λ°μ΄ν°λ² μ΄μ€ μ€ν€λ§
init_db.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sqlite3
import bcrypt
DATABASE = 'database.db'
def hash_password(password):
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
def init_database():
"""λ°μ΄ν°λ² μ΄μ€ μ΄κΈ°ν λ° ν
μ΄λΈ μμ±"""
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
# κΈ°μ‘΄ ν
μ΄λΈ μμ (κ°λ°μ©)
cursor.execute('DROP TABLE IF EXISTS comments')
cursor.execute('DROP TABLE IF EXISTS posts')
cursor.execute('DROP TABLE IF EXISTS users')
# μ¬μ©μ ν
μ΄λΈ
cursor.execute('''
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
role TEXT DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# ν¬μ€νΈ ν
μ΄λΈ
cursor.execute('''
CREATE TABLE posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
category TEXT,
author_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES users(id)
)
''')
# λκΈ ν
μ΄λΈ
cursor.execute('''
CREATE TABLE comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id INTEGER NOT NULL,
author_id INTEGER NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (post_id) REFERENCES posts(id),
FOREIGN KEY (author_id) REFERENCES users(id)
)
''')
# κ΄λ¦¬μ κ³μ μμ±
admin_password = hash_password('admin123')
cursor.execute('''
INSERT INTO users (username, email, password_hash, role)
VALUES (?, ?, ?, ?)
''', ('admin', 'admin@blog.com', admin_password, 'admin'))
# μν μ¬μ©μ
user_password = hash_password('user123')
cursor.execute('''
INSERT INTO users (username, email, password_hash)
VALUES (?, ?, ?)
''', ('νκΈΈλ', 'hong@example.com', user_password))
# μν ν¬μ€νΈ
cursor.execute('''
INSERT INTO posts (title, content, category, author_id)
VALUES (?, ?, ?, ?)
''', ('Flask λΈλ‘κ·Έ μμνκΈ°', 'Flaskλ‘ λΈλ‘κ·Έλ₯Ό λ§λ€μ΄λ΄
μλ€!', 'κΈ°μ ', 1))
cursor.execute('''
INSERT INTO posts (title, content, category, author_id)
VALUES (?, ?, ?, ?)
''', ('Python 100μΌ μ±λ¦°μ§', '100μΌκ°μ Python νμ΅ μ¬μ ', 'μΌμ', 2))
# μν λκΈ
cursor.execute('''
INSERT INTO comments (post_id, author_id, content)
VALUES (?, ?, ?)
''', (1, 2, 'μ’μ κΈ κ°μ¬ν©λλ€!'))
conn.commit()
conn.close()
print("β
λ°μ΄ν°λ² μ΄μ€ μ΄κΈ°ν μλ£!")
print("π κ΄λ¦¬μ: admin / admin123")
print("π€ μ¬μ©μ: νκΈΈλ / user123")
if __name__ == '__main__':
init_database()
μ€ν:
1
python init_db.py
π§ 2λ¨κ³: ν΅μ¬ κΈ°λ₯ ꡬν
app.py (λ©μΈ μ ν리μΌμ΄μ )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
from flask import Flask, render_template, request, redirect, url_for, flash, session, jsonify
import sqlite3
import bcrypt
from datetime import datetime
from functools import wraps
import os
from dotenv import load_dotenv
# νκ²½ λ³μ λ‘λ
load_dotenv()
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', 'dev-secret-key')
DATABASE = 'database.db'
# ========== λ°μ΄ν°λ² μ΄μ€ ν¬νΌ ==========
def get_db():
db = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row
return db
def hash_password(password):
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
def check_password(password, hashed):
return bcrypt.checkpw(password.encode('utf-8'), hashed.encode('utf-8'))
# ========== λ°μ½λ μ΄ν° ==========
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€', 'error')
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
def admin_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€', 'error')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
cursor.execute('SELECT role FROM users WHERE id = ?', (session['user_id'],))
user = cursor.fetchone()
db.close()
if not user or user['role'] != 'admin':
flash('κ΄λ¦¬μλ§ μ κ·Όν μ μμ΅λλ€', 'error')
return redirect(url_for('index'))
return f(*args, **kwargs)
return decorated_function
# ========== λ©μΈ νμ΄μ§ ==========
@app.route('/')
def index():
db = get_db()
cursor = db.cursor()
# μ΅κ·Ό ν¬μ€νΈ
cursor.execute('''
SELECT posts.*, users.username
FROM posts
JOIN users ON posts.author_id = users.id
ORDER BY posts.created_at DESC
LIMIT 5
''')
recent_posts = cursor.fetchall()
# ν΅κ³
cursor.execute('SELECT COUNT(*) as count FROM posts')
total_posts = cursor.fetchone()['count']
cursor.execute('SELECT COUNT(*) as count FROM users')
total_users = cursor.fetchone()['count']
db.close()
return render_template('index.html',
recent_posts=recent_posts,
total_posts=total_posts,
total_users=total_users)
# ========== μΈμ¦ ==========
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form.get('username', '').strip()
email = request.form.get('email', '').strip()
password = request.form.get('password')
password_confirm = request.form.get('password_confirm')
# κ²μ¦
if not username or not email or not password:
flash('λͺ¨λ νλλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ', 'error')
return redirect(url_for('register'))
if password != password_confirm:
flash('λΉλ°λ²νΈκ° μΌμΉνμ§ μμ΅λλ€', 'error')
return redirect(url_for('register'))
# λΉλ°λ²νΈ ν΄μ±
password_hash = hash_password(password)
db = get_db()
cursor = db.cursor()
try:
cursor.execute('''
INSERT INTO users (username, email, password_hash)
VALUES (?, ?, ?)
''', (username, email, password_hash))
db.commit()
flash('νμκ°μ
μ±κ³΅! λ‘κ·ΈμΈν΄μ£ΌμΈμ', 'success')
return redirect(url_for('login'))
except sqlite3.IntegrityError:
flash('μ΄λ―Έ μ‘΄μ¬νλ μμ΄λ λλ μ΄λ©μΌμ
λλ€', 'error')
finally:
db.close()
return render_template('auth/register.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
db = get_db()
cursor = db.cursor()
cursor.execute('SELECT * FROM users WHERE username = ?', (username,))
user = cursor.fetchone()
db.close()
if user and check_password(password, user['password_hash']):
session['user_id'] = user['id']
session['username'] = user['username']
session['role'] = user['role']
flash(f'νμν©λλ€, {username}λ!', 'success')
return redirect(url_for('index'))
else:
flash('μμ΄λ λλ λΉλ°λ²νΈκ° νλ Έμ΅λλ€', 'error')
return render_template('auth/login.html')
@app.route('/logout')
def logout():
session.clear()
flash('λ‘κ·Έμμ λμμ΅λλ€', 'success')
return redirect(url_for('index'))
# ========== ν¬μ€νΈ ==========
@app.route('/posts')
def posts_list():
db = get_db()
cursor = db.cursor()
# κ²μ/νν°
search = request.args.get('search', '')
category = request.args.get('category', '')
query = '''
SELECT posts.*, users.username
FROM posts
JOIN users ON posts.author_id = users.id
WHERE 1=1
'''
params = []
if search:
query += ' AND (posts.title LIKE ? OR posts.content LIKE ?)'
params.extend([f'%{search}%', f'%{search}%'])
if category:
query += ' AND posts.category = ?'
params.append(category)
query += ' ORDER BY posts.created_at DESC'
cursor.execute(query, params)
posts = cursor.fetchall()
db.close()
return render_template('posts/list.html', posts=posts, search=search, category=category)
@app.route('/posts/<int:post_id>')
def post_detail(post_id):
db = get_db()
cursor = db.cursor()
# ν¬μ€νΈ μ‘°ν
cursor.execute('''
SELECT posts.*, users.username
FROM posts
JOIN users ON posts.author_id = users.id
WHERE posts.id = ?
''', (post_id,))
post = cursor.fetchone()
if not post:
db.close()
flash('ν¬μ€νΈλ₯Ό μ°Ύμ μ μμ΅λλ€', 'error')
return redirect(url_for('posts_list'))
# λκΈ μ‘°ν
cursor.execute('''
SELECT comments.*, users.username
FROM comments
JOIN users ON comments.author_id = users.id
WHERE comments.post_id = ?
ORDER BY comments.created_at DESC
''', (post_id,))
comments = cursor.fetchall()
db.close()
return render_template('posts/detail.html', post=post, comments=comments)
@app.route('/posts/create', methods=['GET', 'POST'])
@login_required
def create_post():
if request.method == 'POST':
title = request.form.get('title', '').strip()
content = request.form.get('content', '').strip()
category = request.form.get('category', '').strip()
if not title or not content:
flash('μ λͺ©κ³Ό λ΄μ©μ μ
λ ₯ν΄μ£ΌμΈμ', 'error')
return redirect(url_for('create_post'))
db = get_db()
cursor = db.cursor()
cursor.execute('''
INSERT INTO posts (title, content, category, author_id)
VALUES (?, ?, ?, ?)
''', (title, content, category, session['user_id']))
db.commit()
post_id = cursor.lastrowid
db.close()
flash('ν¬μ€νΈκ° μμ±λμμ΅λλ€', 'success')
return redirect(url_for('post_detail', post_id=post_id))
return render_template('posts/create.html')
@app.route('/posts/<int:post_id>/edit', methods=['GET', 'POST'])
@login_required
def edit_post(post_id):
db = get_db()
cursor = db.cursor()
# κΆν νμΈ
cursor.execute('SELECT * FROM posts WHERE id = ?', (post_id,))
post = cursor.fetchone()
if not post:
db.close()
flash('ν¬μ€νΈλ₯Ό μ°Ύμ μ μμ΅λλ€', 'error')
return redirect(url_for('posts_list'))
if post['author_id'] != session['user_id'] and session.get('role') != 'admin':
db.close()
flash('μμ κΆνμ΄ μμ΅λλ€', 'error')
return redirect(url_for('post_detail', post_id=post_id))
if request.method == 'POST':
title = request.form.get('title', '').strip()
content = request.form.get('content', '').strip()
category = request.form.get('category', '').strip()
cursor.execute('''
UPDATE posts
SET title = ?, content = ?, category = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
''', (title, content, category, post_id))
db.commit()
db.close()
flash('ν¬μ€νΈκ° μμ λμμ΅λλ€', 'success')
return redirect(url_for('post_detail', post_id=post_id))
db.close()
return render_template('posts/edit.html', post=post)
@app.route('/posts/<int:post_id>/delete', methods=['POST'])
@login_required
def delete_post(post_id):
db = get_db()
cursor = db.cursor()
cursor.execute('SELECT * FROM posts WHERE id = ?', (post_id,))
post = cursor.fetchone()
if not post:
db.close()
flash('ν¬μ€νΈλ₯Ό μ°Ύμ μ μμ΅λλ€', 'error')
return redirect(url_for('posts_list'))
if post['author_id'] != session['user_id'] and session.get('role') != 'admin':
db.close()
flash('μμ κΆνμ΄ μμ΅λλ€', 'error')
return redirect(url_for('post_detail', post_id=post_id))
# λκΈλ ν¨κ» μμ
cursor.execute('DELETE FROM comments WHERE post_id = ?', (post_id,))
cursor.execute('DELETE FROM posts WHERE id = ?', (post_id,))
db.commit()
db.close()
flash('ν¬μ€νΈκ° μμ λμμ΅λλ€', 'success')
return redirect(url_for('posts_list'))
# ========== λκΈ ==========
@app.route('/posts/<int:post_id>/comments', methods=['POST'])
@login_required
def add_comment(post_id):
content = request.form.get('content', '').strip()
if not content:
flash('λκΈ λ΄μ©μ μ
λ ₯ν΄μ£ΌμΈμ', 'error')
return redirect(url_for('post_detail', post_id=post_id))
db = get_db()
cursor = db.cursor()
cursor.execute('''
INSERT INTO comments (post_id, author_id, content)
VALUES (?, ?, ?)
''', (post_id, session['user_id'], content))
db.commit()
db.close()
flash('λκΈμ΄ μμ±λμμ΅λλ€', 'success')
return redirect(url_for('post_detail', post_id=post_id))
@app.route('/comments/<int:comment_id>/delete', methods=['POST'])
@login_required
def delete_comment(comment_id):
db = get_db()
cursor = db.cursor()
cursor.execute('SELECT * FROM comments WHERE id = ?', (comment_id,))
comment = cursor.fetchone()
if not comment:
db.close()
flash('λκΈμ μ°Ύμ μ μμ΅λλ€', 'error')
return redirect(url_for('index'))
if comment['author_id'] != session['user_id'] and session.get('role') != 'admin':
db.close()
flash('μμ κΆνμ΄ μμ΅λλ€', 'error')
return redirect(url_for('post_detail', post_id=comment['post_id']))
post_id = comment['post_id']
cursor.execute('DELETE FROM comments WHERE id = ?', (comment_id,))
db.commit()
db.close()
flash('λκΈμ΄ μμ λμμ΅λλ€', 'success')
return redirect(url_for('post_detail', post_id=post_id))
# ========== REST API ==========
@app.route('/api/posts', methods=['GET'])
def api_get_posts():
db = get_db()
cursor = db.cursor()
cursor.execute('''
SELECT posts.*, users.username
FROM posts
JOIN users ON posts.author_id = users.id
ORDER BY posts.created_at DESC
''')
posts = cursor.fetchall()
db.close()
return jsonify({
'count': len(posts),
'posts': [dict(post) for post in posts]
})
@app.route('/api/posts/<int:post_id>', methods=['GET'])
def api_get_post(post_id):
db = get_db()
cursor = db.cursor()
cursor.execute('''
SELECT posts.*, users.username
FROM posts
JOIN users ON posts.author_id = users.id
WHERE posts.id = ?
''', (post_id,))
post = cursor.fetchone()
db.close()
if not post:
return jsonify({'error': 'Post not found'}), 404
return jsonify(dict(post))
# ========== μλ¬ νΈλ€λ¬ ==========
@app.errorhandler(404)
def not_found(error):
return render_template('errors/404.html'), 404
@app.errorhandler(500)
def internal_error(error):
return render_template('errors/500.html'), 500
if __name__ == '__main__':
app.run(debug=True)
π¨ 3λ¨κ³: ν νλ¦Ώ μμ±
templates/base.html (λ² μ΄μ€ ν νλ¦Ώ)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}λΈλ‘κ·Έ{% endblock %}</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; line-height: 1.6; color: #333; background: #f8f9fa; }
.container { max-width: 1200px; margin: 0 auto; padding: 0 20px; }
/* ν€λ */
header { background: #2c3e50; color: white; padding: 1rem 0; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
header .container { display: flex; justify-content: space-between; align-items: center; }
header h1 { font-size: 1.5rem; }
header h1 a { color: white; text-decoration: none; }
header nav a { color: white; margin-left: 1.5rem; text-decoration: none; transition: opacity 0.3s; }
header nav a:hover { opacity: 0.8; }
/* λ©μΈ */
main { padding: 2rem 0; min-height: calc(100vh - 200px); }
/* νλμ λ©μμ§ */
.flash-messages { margin: 1rem 0; }
.flash { padding: 1rem; margin-bottom: 1rem; border-radius: 5px; }
.flash-success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
.flash-error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
/* μΉ΄λ */
.card { background: white; border-radius: 10px; padding: 2rem; margin-bottom: 2rem; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
/* λ²νΌ */
.btn { display: inline-block; padding: 0.75rem 1.5rem; border-radius: 5px; text-decoration: none; border: none; cursor: pointer; font-size: 1rem; transition: all 0.3s; }
.btn-primary { background: #007bff; color: white; }
.btn-primary:hover { background: #0056b3; }
.btn-success { background: #28a745; color: white; }
.btn-success:hover { background: #218838; }
.btn-danger { background: #dc3545; color: white; }
.btn-danger:hover { background: #c82333; }
.btn-secondary { background: #6c757d; color: white; }
.btn-secondary:hover { background: #5a6268; }
/* νΌ */
.form-group { margin-bottom: 1.5rem; }
.form-group label { display: block; margin-bottom: 0.5rem; font-weight: bold; }
.form-group input, .form-group textarea, .form-group select { width: 100%; padding: 0.75rem; border: 1px solid #ddd; border-radius: 5px; font-size: 1rem; }
.form-group textarea { resize: vertical; min-height: 150px; }
/* νΈν° */
footer { background: #2c3e50; color: white; text-align: center; padding: 2rem 0; margin-top: 3rem; }
</style>
</head>
<body>
<header>
<div class="container">
<h1><a href="{{ url_for('index') }}">π My Blog</a></h1>
<nav>
<a href="{{ url_for('index') }}">ν</a>
<a href="{{ url_for('posts_list') }}">κΈ λͺ©λ‘</a>
{% if session.user_id %}
<a href="{{ url_for('create_post') }}">κΈ μ°κΈ°</a>
<a href="{{ url_for('logout') }}">λ‘κ·Έμμ ({{ session.username }})</a>
{% else %}
<a href="{{ url_for('login') }}">λ‘κ·ΈμΈ</a>
<a href="{{ url_for('register') }}">νμκ°μ
</a>
{% endif %}
</nav>
</div>
</header>
<main>
<div class="container">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="flash-messages">
{% for category, message in messages %}
<div class="flash flash-{{ category }}">{{ message }}</div>
{% endfor %}
</div>
{% endif %}
{% endwith %}
{% block content %}{% endblock %}
</div>
</main>
<footer>
<div class="container">
<p>© 2025 My Blog. Made with Flask β€οΈ</p>
<p><a href="/api/posts" style="color: white;">API λ¬Έμ</a></p>
</div>
</footer>
</body>
</html>
templates/index.html (λ©μΈ νμ΄μ§)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{% extends "base.html" %}
{% block content %}
<div class="card" style="text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;">
<h1 style="font-size: 2.5rem; margin-bottom: 1rem;">β¨ νμν©λλ€!</h1>
<p style="font-size: 1.2rem;">Flaskλ‘ λ§λ λΈλ‘κ·Έμ μ€μ κ²μ νμν©λλ€</p>
<div style="display: flex; gap: 2rem; justify-content: center; margin-top: 2rem;">
<div>
<h2 style="font-size: 2rem;">{{ total_posts }}</h2>
<p>μ΄ ν¬μ€νΈ</p>
</div>
<div>
<h2 style="font-size: 2rem;">{{ total_users }}</h2>
<p>μ΄ μ¬μ©μ</p>
</div>
</div>
</div>
<h2 style="margin-bottom: 1.5rem;">π μ΅κ·Ό ν¬μ€νΈ</h2>
{% for post in recent_posts %}
<div class="card">
<h3 style="margin-bottom: 0.5rem;">
<a href="{{ url_for('post_detail', post_id=post['id']) }}" style="color: #333; text-decoration: none;">
{{ post['title'] }}
</a>
</h3>
<p style="color: #666; font-size: 0.9rem; margin-bottom: 1rem;">
{{ post['username'] }} Β· {{ post['created_at'] }}
{% if post['category'] %}
Β· π·οΈ {{ post['category'] }}
{% endif %}
</p>
<p style="color: #555;">{{ post['content'][:200] }}{% if post['content']|length > 200 %}...{% endif %}</p>
<a href="{{ url_for('post_detail', post_id=post['id']) }}" class="btn btn-primary" style="margin-top: 1rem;">μ½κΈ° β</a>
</div>
{% else %}
<div class="card" style="text-align: center; color: #999;">
<p>μμ§ ν¬μ€νΈκ° μμ΅λλ€. 첫 λ²μ§Έ κΈμ μμ±ν΄λ³΄μΈμ!</p>
{% if session.user_id %}
<a href="{{ url_for('create_post') }}" class="btn btn-primary" style="margin-top: 1rem;">κΈ μ°κΈ°</a>
{% endif %}
</div>
{% endfor %}
{% endblock %}
templates/posts/list.html (ν¬μ€νΈ λͺ©λ‘)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{% extends "base.html" %}
{% block title %}κΈ λͺ©λ‘ - λΈλ‘κ·Έ{% endblock %}
{% block content %}
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
<h2>π μ 체 ν¬μ€νΈ ({{ posts|length }}κ°)</h2>
{% if session.user_id %}
<a href="{{ url_for('create_post') }}" class="btn btn-primary">βοΈ μ κΈ μ°κΈ°</a>
{% endif %}
</div>
<!-- κ²μ νΌ -->
<div class="card">
<form method="GET" style="display: flex; gap: 1rem;">
<input type="text" name="search" placeholder="μ λͺ© λλ λ΄μ© κ²μ" value="{{ search }}" style="flex: 2; padding: 0.75rem; border: 1px solid #ddd; border-radius: 5px;">
<select name="category" style="flex: 1; padding: 0.75rem; border: 1px solid #ddd; border-radius: 5px;">
<option value="">λͺ¨λ μΉ΄ν
κ³ λ¦¬</option>
<option value="κΈ°μ " {% if category == 'κΈ°μ ' %}selected{% endif %}>κΈ°μ </option>
<option value="μΌμ" {% if category == 'μΌμ' %}selected{% endif %}>μΌμ</option>
<option value="리뷰" {% if category == '리뷰' %}selected{% endif %}>리뷰</option>
</select>
<button type="submit" class="btn btn-primary">π κ²μ</button>
</form>
</div>
<!-- ν¬μ€νΈ λͺ©λ‘ -->
{% for post in posts %}
<div class="card">
<h3 style="margin-bottom: 0.5rem;">
<a href="{{ url_for('post_detail', post_id=post['id']) }}" style="color: #333; text-decoration: none;">
{{ post['title'] }}
</a>
</h3>
<p style="color: #666; font-size: 0.9rem; margin-bottom: 1rem;">
π€ {{ post['username'] }} Β· π
{{ post['created_at'] }}
{% if post['category'] %}Β· π·οΈ {{ post['category'] }}{% endif %}
</p>
<p style="color: #555;">{{ post['content'][:300] }}{% if post['content']|length > 300 %}...{% endif %}</p>
<a href="{{ url_for('post_detail', post_id=post['id']) }}" class="btn btn-primary" style="margin-top: 1rem;">μμΈν 보기 β</a>
</div>
{% else %}
<div class="card" style="text-align: center; color: #999;">
<h3>κ²μ κ²°κ³Όκ° μμ΅λλ€</h3>
<p>λ€λ₯Έ ν€μλλ‘ κ²μν΄λ³΄μΈμ</p>
</div>
{% endfor %}
{% endblock %}
templates/posts/detail.html (ν¬μ€νΈ μμΈ)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{% extends "base.html" %}
{% block title %}{{ post['title'] }} - λΈλ‘κ·Έ{% endblock %}
{% block content %}
<div class="card">
<h1 style="margin-bottom: 1rem;">{{ post['title'] }}</h1>
<p style="color: #666; margin-bottom: 2rem;">
π€ {{ post['username'] }} Β· π
{{ post['created_at'] }}
{% if post['category'] %}Β· π·οΈ {{ post['category'] }}{% endif %}
</p>
<div style="line-height: 1.8; white-space: pre-wrap;">{{ post['content'] }}</div>
{% if session.user_id and (session.user_id == post['author_id'] or session.role == 'admin') %}
<div style="margin-top: 2rem; padding-top: 2rem; border-top: 1px solid #ddd; display: flex; gap: 1rem;">
<a href="{{ url_for('edit_post', post_id=post['id']) }}" class="btn btn-secondary">βοΈ μμ </a>
<form method="POST" action="{{ url_for('delete_post', post_id=post['id']) }}" style="display: inline;">
<button type="submit" class="btn btn-danger" onclick="return confirm('μ λ§ μμ νμκ² μ΅λκΉ?')">ποΈ μμ </button>
</form>
</div>
{% endif %}
</div>
<!-- λκΈ μΉμ
-->
<div class="card">
<h2 style="margin-bottom: 1.5rem;">π¬ λκΈ ({{ comments|length }}κ°)</h2>
{% if session.user_id %}
<form method="POST" action="{{ url_for('add_comment', post_id=post['id']) }}" style="margin-bottom: 2rem;">
<div class="form-group">
<textarea name="content" placeholder="λκΈμ μ
λ ₯νμΈμ" required></textarea>
</div>
<button type="submit" class="btn btn-primary">λκΈ μμ±</button>
</form>
{% else %}
<p style="text-align: center; color: #999; padding: 2rem; background: #f8f9fa; border-radius: 5px;">
λκΈμ μμ±νλ €λ©΄ <a href="{{ url_for('login') }}">λ‘κ·ΈμΈ</a>ν΄μ£ΌμΈμ
</p>
{% endif %}
{% for comment in comments %}
<div style="border-left: 3px solid #007bff; padding-left: 1rem; margin-bottom: 1.5rem;">
<p style="font-weight: bold; margin-bottom: 0.5rem;">
{{ comment['username'] }}
<span style="font-weight: normal; color: #666; font-size: 0.9rem;">Β· {{ comment['created_at'] }}</span>
</p>
<p style="white-space: pre-wrap;">{{ comment['content'] }}</p>
{% if session.user_id and (session.user_id == comment['author_id'] or session.role == 'admin') %}
<form method="POST" action="{{ url_for('delete_comment', comment_id=comment['id']) }}" style="margin-top: 0.5rem;">
<button type="submit" class="btn btn-danger" style="font-size: 0.9rem; padding: 0.4rem 1rem;" onclick="return confirm('λκΈμ μμ νμκ² μ΅λκΉ?')">μμ </button>
</form>
{% endif %}
</div>
{% else %}
<p style="text-align: center; color: #999;">μμ§ λκΈμ΄ μμ΅λλ€</p>
{% endfor %}
</div>
<a href="{{ url_for('posts_list') }}" class="btn btn-secondary">β λͺ©λ‘μΌλ‘</a>
{% endblock %}
π μμ±!
μ€ννκΈ°
1
2
3
4
5
6
7
8
# λ°μ΄ν°λ² μ΄μ€ μ΄κΈ°ν
python init_db.py
# μλ² μ€ν
python app.py
# λΈλΌμ°μ μμ μ μ
# http://localhost:5000
ν μ€νΈ κ³μ
1
2
3
4
5
6
7
κ΄λ¦¬μ:
- μμ΄λ: admin
- λΉλ°λ²νΈ: admin123
μΌλ° μ¬μ©μ:
- μμ΄λ: νκΈΈλ
- λΉλ°λ²νΈ: user123
π λ§λ¬΄λ¦¬
Phase 9μμ λ°°μ΄ κ²λ€
1
2
3
4
5
6
7
8
9
10
β
Day 81: Flask κΈ°μ΄
β
Day 82: λΌμ°ν
β
Day 83: ν
νλ¦Ώ
β
Day 84: νΌ μ²λ¦¬
β
Day 85: λ°μ΄ν°λ² μ΄μ€
β
Day 86: CRUD
β
Day 87: REST API
β
Day 88: μ¬μ©μ μΈμ¦
β
Day 89: λ°°ν¬ μ€λΉ
β
Day 90: μ’
ν© νλ‘μ νΈ
λ€μ λ¨κ³
1
2
3
4
5
6
7
8
9
10
11
12
# νλ‘μ νΈ κ°μ μμ΄λμ΄:
1. νμΌ μ
λ‘λ (μ΄λ―Έμ§ 첨λΆ)
2. νμ΄μ§λ€μ΄μ
(ν νμ΄μ§μ 10κ°μ©)
3. μ’μμ κΈ°λ₯
4. νκ·Έ μμ€ν
5. μ΄λ©μΌ μΈμ¦
6. μμ
λ‘κ·ΈμΈ (Google, GitHub)
7. λ§ν¬λ€μ΄ μ§μ
8. κ²μ κ³ λν
9. μλ¦Ό μμ€ν
10. μ€μκ° μ±ν
π μΆνν©λλ€!
Phase 9: μΉ κ°λ° μ λ¬Έμ μλ£νμ΅λλ€!
10μΌ λμ Flaskλ‘ μμ ν μΉ μ ν리μΌμ΄μ μ λ§λλ λ°©λ²μ λ°°μ μ΅λλ€. μ΄μ μ¬λ¬λΆμ μμ λ§μ μΉμ¬μ΄νΈλ₯Ό λ§λ€ μ μμ΅λλ€!
λ€μ Phaseμμ λ΅κ² μ΅λλ€! π
βμμ±λ νλ‘μ νΈλ μμμ λμ΄μ, μλ‘μ΄ μμμ μμμ λλ€!β π―
Day 90/100 Phase 9: μΉ κ°λ° μ λ¬Έ μλ£! #100DaysOfPython
