[Python] Python (no Django/Flask) - CSS and other static files not being recognized in base...

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    Apologies for the basic question, but I can't figure out why the CSS sheet called out in my base template's header isn't hooked up when I check my index.html file on http.server.

    I'm migrating my site to Python and Jinja, no frameworks.

    I built a server module/script based on some tutorials I found online:

    # server.py
    import os
    import http.server
    import socketserver
    from jinja2 import Environment, FileSystemLoader

    PORT = 8000
    STATIC_DIR = "static/"

    class StaticFileHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
    super().__init__(*args, directory=STATIC_DIR, **kwargs)

    with socketserver.TCPServer(("", PORT), StaticFileHandler) as httpd:
    print(f"Serving at http://localhost:{PORT}/")
    httpd.serve_forever()


    The base template's header looks like this (omitting the rest of the template's code as I don't think it's relevant here):

    <!DOCTYPE html>
    <html lang="en-us">
    <head>
    <meta charset="utf-8">
    <title>{% block title %}{% endblock title %}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="noai, noimageai, noarchive">
    <link rel="icon" type="image/x-icon" href="favicon.ico">
    <link rel="stylesheet" href="styles.css">
    </head>


    I've also tried writing the paths to the static files as "static/styles.css" and so on, but no luck there. What am I missing? I feel like (and hope) this is just some dumb oversight that's right under my nose and I'm not seeing it, but...well, I've wracked my brain on this and just can't see it. Any ideas?

    Also: in server.py, what do I enter for STATIC_DIR if I want the project's home folder to be the static directory (just for testing purposes for now)? I thought leaving it blank or just adding / would do the trick, but those seem to show me my computer's root directory, not the project's, and obviously I don't want that.

    Perhaps a better question is: how would I set up server.py so it pulls static files like CSS, fonts, etc. from STATIC_DIR, but looks for index.html from a different directory (like the project folder, or maybe a site folder since I plan on making an output folder for all the HTML files to upload, like a static site generator?

    EDIT: I should include my current test script for generating the index.html file. It saves the HTML file to the project folder, but I just drag it to static for testing:

    # write-posts.py
    from jinja2 import Environment, FileSystemLoader
    from datetime import datetime, timezone

    # date = datetime(tzinfo=timezone.utc).isoformat()
    posts = [
    {"title": "First Post", "slug": "first-post", "date": "2025, 7, 1", "body": "First post!", "tags": "First Post"},
    ]

    environment = Environment(loader=FileSystemLoader("templates"))
    template = environment.get_template("index.html")
    filename = "index.html"

    content = template.render(posts=posts)

    with open(filename, mode="w", encoding="utf-8") as message:
    message.write(content)
    print(f"... wrote {filename}")

    Continue reading...

Compartilhe esta Página