-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
34 lines (28 loc) · 1.05 KB
/
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
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
# Sample data (for demonstration purposes)
posts = [
{"id": 1, "title": "First Post", "content": "This is the content of the first post."},
{"id": 2, "title": "Second Post", "content": "This is the content of the second post."},
]
@app.route('/')
def index():
return render_template('index.html', posts=posts)
@app.route('/post/<int:post_id>')
def view_post(post_id):
post = next((p for p in posts if p['id'] == post_id), None)
if post:
return render_template('post.html', post=post)
else:
return "Post not found."
@app.route('/create', methods=['GET', 'POST'])
def create_post():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
new_post = {"id": len(posts) + 1, "title": title, "content": content}
posts.append(new_post)
return redirect(url_for('index'))
return render_template('create_post.html')
if __name__ == '__main__':
app.run(debug=True)