Simple Blog with Flask
Abstract
Create a Simple Blog application using Flask. The app displays a list of blog posts, allows adding new posts, and viewing individual posts. This project demonstrates web development, routing, and template rendering in Python.
Prerequisites
- Python 3.6 or above
- Text Editor or IDE
- Basic understanding of Python syntax
- Familiarity with Flask for web development
- Knowledge of HTML templates
Getting Started
Creating a new project
- Create a new project folder and name it
simple_blog_with_flask
simple_blog_with_flask
. - Create a new file inside the folder and name it
simple_blog_with_flask.py
simple_blog_with_flask.py
. - Open the project folder in your favorite text editor or IDE.
- Copy the code below and paste it into the
simple_blog_with_flask.py
simple_blog_with_flask.py
file.
Write the code
⚙️ simple_blog_with_flask.py
simple_blog_with_flask.py
"""
Simple Blog with Flask
A Python application that simulates a simple blog using Flask.
Features include:
- Displaying a list of blog posts.
- Adding new blog posts.
- Viewing individual blog posts.
"""
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
# Sample data for blog posts
blog_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():
"""Display the list of blog posts."""
return render_template('index.html', posts=blog_posts)
@app.route('/post/<int:post_id>')
def view_post(post_id):
"""View an individual blog post."""
post = next((p for p in blog_posts if p['id'] == post_id), None)
if post:
return render_template('post.html', post=post)
return "Post not found", 404
@app.route('/new', methods=['GET', 'POST'])
def new_post():
"""Add a new blog post."""
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
new_id = max(p['id'] for p in blog_posts) + 1 if blog_posts else 1
blog_posts.append({"id": new_id, "title": title, "content": content})
return redirect(url_for('index'))
return render_template('new_post.html')
if __name__ == '__main__':
app.run(debug=True)
simple_blog_with_flask.py
"""
Simple Blog with Flask
A Python application that simulates a simple blog using Flask.
Features include:
- Displaying a list of blog posts.
- Adding new blog posts.
- Viewing individual blog posts.
"""
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
# Sample data for blog posts
blog_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():
"""Display the list of blog posts."""
return render_template('index.html', posts=blog_posts)
@app.route('/post/<int:post_id>')
def view_post(post_id):
"""View an individual blog post."""
post = next((p for p in blog_posts if p['id'] == post_id), None)
if post:
return render_template('post.html', post=post)
return "Post not found", 404
@app.route('/new', methods=['GET', 'POST'])
def new_post():
"""Add a new blog post."""
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
new_id = max(p['id'] for p in blog_posts) + 1 if blog_posts else 1
blog_posts.append({"id": new_id, "title": title, "content": content})
return redirect(url_for('index'))
return render_template('new_post.html')
if __name__ == '__main__':
app.run(debug=True)
Key Features
- Display a list of blog posts
- Add new blog posts
- View individual blog posts
- Web interface using Flask and HTML templates
Explanation
Displaying Blog Posts
The app uses a list of dictionaries to store blog posts and renders them using Flask templates:
simple_blog_with_flask.py
blog_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."},
]
simple_blog_with_flask.py
blog_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."},
]
Adding New Posts
Users can add new posts via a form. The new post is appended to the list and the user is redirected to the main page:
simple_blog_with_flask.py
@app.route('/new', methods=['GET', 'POST'])
def new_post():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
new_id = max(p['id'] for p in blog_posts) + 1 if blog_posts else 1
blog_posts.append({"id": new_id, "title": title, "content": content})
return redirect(url_for('index'))
return render_template('new_post.html')
simple_blog_with_flask.py
@app.route('/new', methods=['GET', 'POST'])
def new_post():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
new_id = max(p['id'] for p in blog_posts) + 1 if blog_posts else 1
blog_posts.append({"id": new_id, "title": title, "content": content})
return redirect(url_for('index'))
return render_template('new_post.html')
Viewing Individual Posts
Each post can be viewed by visiting its unique URL:
simple_blog_with_flask.py
@app.route('/post/<int:post_id>')
def view_post(post_id):
post = next((p for p in blog_posts if p['id'] == post_id), None)
if post:
return render_template('post.html', post=post)
return "Post not found", 404
simple_blog_with_flask.py
@app.route('/post/<int:post_id>')
def view_post(post_id):
post = next((p for p in blog_posts if p['id'] == post_id), None)
if post:
return render_template('post.html', post=post)
return "Post not found", 404
Running the Application
- Save the file.
- Install Flask:
pip install flask
pip install flask
- Run the application:
python simple_blog_with_flask.py
python simple_blog_with_flask.py
Conclusion
This Simple Blog with Flask project is a great way to learn about web development and routing in Python. You can extend it by adding user authentication, comments, or a database backend.
Was this page helpful?
Let us know how we did