首页 > 解决方案 > 从文件输出读取到 django 中的 html 模板

问题描述

下面链接的解决方案如何在views.py中编写以从多个文件中读取并输出这些不同的字段?我可以做到这一点,但如果我尝试复制粘贴 f = open('path/text.txt', 'r') file_content = f.read() f.close() context = {'file_content': file_content}

并稍微改变字母以使其具有独特的版本,这似乎打破了它......我正在传递独特的变体而不是重复使用“f”例如:h = open(BASE_DIR, 'path/tofile') file_content = h.read() h .close() 上下文 = {'file_contenth': file_contenth}

然后我传入返回 return render(request, pathtohtml, {'file_content': file_content}, {'file_contenth': file_contenth}

这会“破坏”它。我尝试了几种将这些变量传递给无济于事的变体

我使用在这里找到的解决方案 Django: Display contents of txt file on the website

标签: pythonpython-3.xdjangonginx

解决方案


正确的格式如下:

return render(request, path/to/html, {'file_content': file_content, 'file_contenth': file_contenth, 'other_variable': other_variable})

推荐阅读