【问题标题】:Read from file output to html template in django从文件输出读取到 django 中的 html 模板
【发布时间】:2021-12-17 02:41:07
【问题描述】:

如何将下面链接的解决方案写入 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() context = {'file_contenth': file_contenth}

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

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

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

【问题讨论】:

    标签: python python-3.x django nginx


    【解决方案1】:

    正确的格式如下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-02
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      相关资源
      最近更新 更多