【问题标题】:Access local html files in Django在 Django 中访问本地 html 文件
【发布时间】:2019-06-05 08:37:59
【问题描述】:

我想从我的 Django 应用程序访问本地 html 文件。

这些文件是在 webapp 使用过程中生成的。

怎么办? 我应该将它们存储在特定文件夹中吗?

非常感谢

【问题讨论】:

  • 您是否阅读过有关模板的 Django 文档? docs.djangoproject.com/en/2.2/intro/overview/…
  • 这些文件是在使用 webapp 的过程中生成的。 - 这是什么意思?
  • 这意味着是计算的结果,由 webapp 启动。
  • “访问”它们是什么意思?你想用它们做什么?

标签: python html django


【解决方案1】:

对于谁可能关心的问题,我通过包含在模板中生成的 html 文件来解决它。

view.py

filepath = 'html_file.html'
with open(filepath,'r') as f:
    content = f.readlines()
context = {'html_content': content}
return render(request, 'template.html', context)

模板.html

{{ html_content|safe }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多