手里有一个django网站,因为页面内容不会发生改变,当客户访问页面时,在本地生成静态html页面,下次再访问直接访问静态html
def juhe_detail(request, slug):
# 获取存放静态html文件夹
GENERATED_STATIC_HTML_FILES_DIR = os.path.join(os.path.abspath(settings.BASE_DIR), 'templates', 'juhe08','static_html')
# 获取生成静态文件路径
file_path = os.path.join(GENERATED_STATIC_HTML_FILES_DIR, slug + '.html')
# 判断是否有该文件
if not os.path.exists(file_path):
content = render_to_string('juhe08/juhe.html', locals())
with open(file_path, 'w', encoding='utf-8') as static_file:
static_file.write(content)
return render(request, file_path)
我尝试将静态文件路径放在其他目录,但是django会报错,放在templates下没有问题,这个以后在研究