Python

How to Create a Basic Python Blog


Introduction

Flask is a lightweight and flexible Python web framework that's perfect for creating web applications like blogs. In this guide, we'll explore how to create a basic Python blog using Flask. We'll provide sample code to demonstrate the process.

Prerequisites

Before you start building a Python blog with Flask, ensure you have the following prerequisites:

  • Python and Flask installed on your system.
  • Basic knowledge of Python and web development.

Creating a Basic Python Blog with Flask

Let's create a basic Python blog using Flask with features like creating and displaying blog posts. This example is a simplified version, and you can expand it by adding more features.

from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
# Sample blog post data
posts = [
    {'title': 'First Blog Post', 'content': 'This is the content of the first blog post.'},
    {'title': 'Second Blog Post', 'content': 'Here is the content of the second blog post.'}
]
# Route to display all blog posts
@app.route('/')
def display_posts():
    return render_template('index.html', posts=posts)
# Route to create a new blog post
@app.route('/create', methods=['GET', 'POST'])
def create_post():
    if request.method == 'POST':
        title = request.form['title']
        content = request.form['content']
        new_post = {'title': title, 'content': content}
        posts.append(new_post)
        return redirect(url_for('display_posts'))
    return render_template('create.html')
if __name__ == '__main__':
    app.run(debug=True)
        

Creating HTML Templates

To display and create blog posts, you'll need HTML templates. Here are the templates for displaying all posts and creating new posts:

<!-- index.html -->
<html>
<head>
    <title>Python Blog</title>
</head>
<body>
    <h1>Welcome to the Python Blog</h1>
    <ul>
        {% for post in posts %}
            <li><h2>{{ post.title }}</h2> {{ post.content }}</li>
        {% endfor %}
    </ul>
    <a href=`{{ url_for('create_post') }}`>Create a New Post</a>
</body>
</html>
<!-- create.html -->
<html>
<head>
    <title>Create a New Post</title>
</head>
<body>
    <h1>Create a New Blog Post</h1>
    <form method=`POST`>
        <label>Title:</label>
        <input type=`text` name=`title` required><br>
        <label>Content:</label>
        <textarea name=`content` required></textarea><br>
        <input type=`submit` value=`Create Post`>
    </form>
    <a href=`{{ url_for('display_posts') }}`>Back to All Posts</a>
</body>
</html>
        

Running the Blog

After setting up the Flask app and creating the templates, you can run your Python blog and access it in a web browser.

# Run the Flask app
python your_app.py
        

Open a web browser and visit http://localhost:5000 to see your Python blog.

Conclusion

Creating a basic Python blog using Flask is a great way to start building web applications. You can expand this project by adding features like user authentication, comments, and more advanced templates.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.