Flask

Flask Blogging - Building a Simple Blog


Introduction

Creating a blog is a common project for web developers and content creators. In this guide, we'll explore how to build a simple blog using Flask, a Python web framework. You'll learn how to set up a blog, create and manage blog posts, store posts in a database, and display them on a webpage. By following this guide, you'll have the foundation for creating your own blog, and you can extend it with features like user accounts, comments, and more advanced styling.

Step 1: Setting Up Your Flask Application

Start by setting up your Flask application and creating a directory structure. Here's a sample structure:

simple-blog/
    app.py
    templates/
        index.html
        post.html
    

Step 2: Creating the Blog Application

Create a Flask application for the blog. Here's an example of the Python code:

# app.py
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'
db = SQLAlchemy(app)
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    content = db.Column(db.Text, nullable=False)
@app.route('/')
def index():
    posts = Post.query.all()
    return render_template('index.html', posts=posts)
@app.route('/post/<int:id>')
def post(id):
    post = Post.query.get(id)
    return render_template('post.html', post=post)
if __name__ == '__main__':
    app.run(debug=True)
        

Step 3: Creating HTML Templates

Create HTML templates to display the blog posts and individual blog post pages. Here's an example:

<!-- templates/index.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Simple Blog</title>
</head>
<body>
    <header>
        <h1>Simple Blog</h1>
    </header>
    <section>
        <h2>Recent Posts</h2>
        <ul>
            {% for post in posts %}
                <li><a href=`/post/{{ post.id }}`>{{ post.title }}</a></li>
            {% endfor %}
        </ul>
    </section>
</body>
</html>
        
<!-- templates/post.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{{ post.title }}</title>
</head>
<body>
    <header>
        <h1>{{ post.title }}</h1>
    </header>
    <section>
        <p>{{ post.content }}</p>
    </section>
</body>
</html>
        

Step 4: Running Your Blog Application

Run your Flask blog application using the following command:

python app.py
        

Access your blog in a web browser and start adding and viewing blog posts.

Conclusion

Building a simple blog using Flask is a great way to get started with web development. By following the steps in this guide, you can set up a blog, create blog posts, store them in a database, and display them on a webpage. This project serves as a foundation for creating more advanced blogs with features like user accounts, comments, categories, and styling customization.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.