Simple Blog with Flask
Abstract
Simple Blog with Flask is a Python web application that allows users to create, view, and manage blog posts. It demonstrates web development, routing, and template rendering with Flask. This project is ideal for learning about backend development, HTTP requests, and dynamic web pages.
Prerequisites
- Python 3.6 or above
- Flask (
pip install flask
pip install flask
)
Before you Start
Install Python and Flask. Familiarize yourself with basic web concepts.
Getting Started
- Create a folder named
simple-blog
simple-blog
. - Create a file named
simple_blog_with_flask.py
simple_blog_with_flask.py
. - Copy the code below into your file.
⚙️ Simple Blog with Flask
Simple Blog with Flask
"""
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
"""
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)
- Run the script:
python simple_blog_with_flask.py
python simple_blog_with_flask.py
Explanation
Code Breakdown
- Import modules
from flask import Flask, render_template, request, redirect
from flask import Flask, render_template, request, redirect
- Set up Flask app
app = Flask(__name__)
app = Flask(__name__)
- Define routes
@app.route('/')
def home():
# Show all posts
pass
@app.route('/add', methods=['POST'])
def add_post():
# Add new post
pass
@app.route('/')
def home():
# Show all posts
pass
@app.route('/add', methods=['POST'])
def add_post():
# Add new post
pass
- Run the app
if __name__ == '__main__':
app.run(debug=True)
if __name__ == '__main__':
app.run(debug=True)
Features
- Create and view blog posts
- Simple web interface
- Demonstrates Flask routing
- Easy to extend for more features
How It Works
- Handles HTTP requests
- Renders templates for pages
- Stores posts in memory or file
GUI Components
- Home page: Lists posts
- Form: For adding new posts
- Buttons: For navigation
Use Cases
- Personal blogging
- Learning web development
- Building portfolio sites
Next Steps
You can enhance this project by:
- Adding user authentication
- Storing posts in a database
- Supporting comments
- Improving UI with CSS
- Deploying online
Enhanced Version Ideas
def add_database_support():
# Use SQLite or PostgreSQL
pass
def add_user_login():
# Require login for posting
pass
def add_database_support():
# Use SQLite or PostgreSQL
pass
def add_user_login():
# Require login for posting
pass
Troubleshooting Tips
- Flask errors: Check installation
- App not running: Check port and debug mode
- Posts not saving: Check storage logic
Conclusion
This project teaches Flask basics, routing, and web app structure. Extend it for more features and production use.
Was this page helpful?
Let us know how we did