Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

create a blog website with python

  1. User

    create a blog website with python

  2. Assistant

    Creating a simple blog website with Python can be done efficiently using a web framework like Flask or Django. Flask is lightweight and great for beginners, while Django is more feature-rich and better if you want an all-in-one solution including admin interface, ORM, etc.

    Here, I'll guide you through creating a basic blog website using Flask since it's easier to get started with, and you can expand it later.


    Step 1: Set Up Your Environment

    Make sure you have Python installed (preferably 3.6 or newer).

    Create a folder for your project and inside it create a virtual environment (optional but recommended):

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    

    Install Flask:

    pip install Flask
    

    Step 2: Create the Flask Application

    Create a structure like:

    myblog/
    ├── app.py
    ├── templates/
    │   ├── base.html
    │   ├── index.html
    │   ├── post.html
    │   ├── new_post.html
    

    Step 3: Write the Code

    app.py

    from flask import Flask, render_template, request, redirect, url_for
    
    app = Flask(__name__)
    
    # In-memory blog posts storage (dictionary or list)
    posts = [
        {
            'id': 1,
            'title': 'First Post',
            'content': 'This is my first blog post!'
        },
        {
            'id': 2,
            'title': 'Another Post',
            'content': 'Here is some more content.'
        }
    ]
    
    def get_post(post_id):
        for post in posts:
            if post['id'] == post_id:
                return post
        return None
    
    @app.route('/')
    def index():
        return render_template('index.html', posts=posts)
    
    @app.route('/post/<int:post_id>')
    def post(post_id):
        post = get_post(post_id)
        if not post:
            return "Post not found", 404
        return render_template('post.html', post=post)
    
    @app.route('/new', methods=['GET', 'POST'])
    def new_post():
        if request.method == 'POST':
            title = request.form['title']
            content = request.form['content']
            if not title or not content:
                error = "Title and content cannot be empty."
                return render_template('new_post.html', error=error)
            new_id = posts[-1]['id'] + 1 if posts else 1
            posts.append({'id': new_id, 'title': title, 'content': content})
            return redirect(url_for('post', post_id=new_id))
        return render_template('new_post.html')
    
    if __name__ == '__main__':
        app.run(debug=True)
    

    Step 4: Create HTML Templates

    You can use Jinja2 templates which Flask uses.

    templates/base.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}My Blog{% endblock %}</title>
        <style>
            body { font-family: Arial, sans-serif; max-width: 700px; margin: auto; padding: 1em; }
            header { margin-bottom: 2em; }
            nav a { margin-right: 1em; }
            article { border-bottom: 1px solid #ddd; padding-bottom: 1em; margin-bottom: 1em; }
        </style>
    </head>
    <body>
        <header>
            <h1><a href="{{ url_for('index') }}" style="text-decoration:none;color:black;">My Blog</a></h1>
            <nav>
                <a href="{{ url_for('index') }}">Home</a>
                <a href="{{ url_for('new_post') }}">New Post</a>
            </nav>
        </header>
        {% with messages = get_flashed_messages() %}
          {% if messages %}
            <ul>
              {% for message in messages %}
                <li>{{ message }}</li>
              {% endfor %}
            </ul>
          {% endif %}
        {% endwith %}
        {% block content %}{% endblock %}
    </body>
    </html>
    

    templates/index.html

    {% extends "base.html" %}
    {% block title %}Home - My Blog{% endblock %}
    {% block content %}
      <h2>Posts</h2>
      {% if posts %}
        {% for post in posts %}
          <article>
            <h3><a href="{{ url_for('post', post_id=post.id) }}">{{ post.title }}</a></h3>
            <p>{{ post.content[:100] }}{% if post.content|length > 100 %}...{% endif %}</p>
          </article>
        {% endfor %}
      {% else %}
        <p>No posts yet.</p>
      {% endif %}
    {% endblock %}
    

    templates/post.html

    {% extends "base.html" %}
    {% block title %}{{ post.title }} - My Blog{% endblock %}
    {% block content %}
      <article>
        <h2>{{ post.title }}</h2>
        <p>{{ post.content }}</p>
      </article>
      <p><a href="{{ url_for('index') }}">Back to Home</a></p>
    {% endblock %}
    

    templates/new_post.html

    {% extends "base.html" %}
    {% block title %}New Post - My Blog{% endblock %}
    {% block content %}
      <h2>Create a New Post</h2>
      {% if error %}
        <p style="color:red;">{{ error }}</p>
      {% endif %}
      <form method="post">
        <p>
          <label for="title">Title:</label><br />
          <input type="text" name="title" id="title" size="40" />
        </p>
        <p>
          <label for="content">Content:</label><br />
          <textarea name="content" id="content" rows="10" cols="60"></textarea>
        </p>
        <p><input type="submit" value="Create Post" /></p>
      </form>
    {% endblock %}
    

    Step 5: Run the App

    In your terminal:

    python app.py
    

    Open your web browser at http://127.0.0.1:5000/ and you should see your blog!


    Notes to Improve / Extend

    • Currently, blog posts are stored in-memory — meaning all posts are lost if the server restarts.
    • To persist posts, connect a database. For SQLite integration, use Flask extensions like Flask-SQLAlchemy.
    • Add user authentication.
    • Add Markdown support for content formatting.
    • Allow editing and deleting posts.
    • Improve styling with CSS.

    If you'd like, I can help you extend this for database integration or add user accounts. Just ask!