Я перемещаю свой сайт на Python и jinja, нет Framewrpt. На основе некоторых уроков, которые я нашел в Интернете: < /p>
Код: Выделить всё
# 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()
< /code>
Заголовок базового шаблона выглядит так (пропуская остальную часть кода шаблона, так как я не думаю, что он здесь актуален): < /p>
{% block title %}{% endblock title %}
Код: Выделить всё
# 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}")
< /code>
Редактировать 2: Я попробовал предложения Фураса, но я все еще не получаю статические файлы для подключения. Вот мои обновленные файлы:
[b] server.py[/b]
import os
import http.server
import socketserver
from jinja2 import Environment, FileSystemLoader
PORT = 8000
BASE_DIR = "source/" # Directory containing static files. "BASE_DIR="/full/path/to/www/" might be better if server runs from a different working directory.
class StaticFileHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=BASE_DIR, **kwargs)
with socketserver.TCPServer(("", PORT), StaticFileHandler) as httpd:
print(f"Serving at http://localhost:{PORT}/")
httpd.serve_forever()
Код: Выделить всё
{% block title %}{% endblock title %}
Код: Выделить всё
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("source/templates"))
template = environment.get_template("index.html")
filename = "index.html"
Код: Выделить всё
src: url("static/fonts/font-example.woff2") format("woff2");
< /code>
Я получаю эти сообщения в терминале, когда загружаю страницу в моем браузере: < /p>
127.0.0.1 - - [18/Jul/2025 10:22:56] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [18/Jul/2025 10:23:05] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [18/Jul/2025 10:23:05] code 404, message File not found
127.0.0.1 - - [18/Jul/2025 10:23:05] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [18/Jul/2025 10:29:44] "GET / HTTP/1.1" 304 -
Есть идеи?
Подробнее здесь: https://stackoverflow.com/questions/797 ... ized-in-ba