[ Django生成静态文件模板 ]


Django静态文件生成

from collections import OrderedDict
from django.conf import settings
from django.template import loader
import os

# 静态文件生成
def generate_static_index_html(context: OrderedDict):
    try:
        template = loader.get_template(template_name='tun.html')
        html = template.render(context=context)
        file_path = os.path.join(
            settings.GEBERATED_STATIC_HTML_FILES_DIR, 'index_static.html')
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(html)
        return 1
    except Exception as e:
        print(e)
        return 0

tmeplate

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试网</title>
</head>
<body>
    <h2>{{ name }}</h2>
</body>
</html>