简体   繁体   English

从文件输出读取到 django 中的 html 模板

[英]Read from file output to html template in django

How would the solution linked below be written in views.py to read from multiple files and output those various fields?下面链接的解决方案将如何写入 views.py 以从多个文件中读取并输出这些不同的字段? I can make this work for one but if I try copy pasting f = open('path/text.txt', 'r') file_content = f.read() f.close() context = {'file_content': file_content}我可以为一个人做这项工作,但如果我尝试复制粘贴 f = open('path/text.txt', 'r') file_content = f.read() f.close() context = {'file_content': file_content}

and changing the letters slightly to have unique versions it seems to break it...I am passing in unique variations not reusing "f" ex: h = open(BASE_DIR, 'path/tofile') file_content = h.read() h.close() context = {'file_contenth': file_contenth}并稍微改变字母以获得独特的版本它似乎打破了它......我正在传递独特的变化而不是重复使用“f”例如:h = open(BASE_DIR, 'path/tofile') file_content = h.read() h .close() 上下文 = {'file_contenth': file_contenth}

I then pass in to the return return render(request, pathtohtml, {'file_content': file_content}, {'file_contenth': file_contenth}然后我传入 return 返回 render(request, pathtohtml, {'file_content': file_content}, {'file_contenth': file_contenth}

and that "breaks" it.这“打破”了它。 I've tried a few variations of passing those variables in to no avail我尝试了一些传递这些变量的变体,但无济于事

I used the solution found here Django: Display contents of txt file on the website我使用了在此处找到的解决方案Django: Display contents of txt file on the website

正确的格式如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM