[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', '[email protected]', admin_password, 'admin'))
# ์ํ ์ฌ์ฉ์
user_password = hash_password('user123')
cursor.execute('''
INSERT INTO users (username, email, password_hash)
VALUES (?, ?, ?)
''', ('ํ๊ธธ๋', '[email protected]', 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
