【发布时间】:2020-03-30 02:09:48
【问题描述】:
我正在尝试在简单网站中创建一个文件,然后在 Django 视图函数内的变量中读取相同的内容,并将变量解析为要在网页上显示的模板。 但是,当我打印变量时,它在 cmd 上的显示与原始文本文件中的相同,但网页上的输出没有格式,而是显示为单个字符串。
我已经坚持了两天了。
另外我对 django 还比较陌生,并且可以自学
file1 = open(r'status.txt','w',encoding='UTF-8')
file1.seek(0)
for i in range(0,len(data.split())):
file1.write(data.split()[i] + " ")
if i%5==0 and i!=0 and i!=5:
file1.write("\n")
file1.close()
file1 = open(r'status.txt',"r+",encoding='UTF-8')
d = file1.read()
print(d) #prints on cmd in the same formatting as in text file
return render(request,'status.html',{'dat':d}) **#the html displays it only as a single text
string**
<body>
{% block content %}
{{dat}}
{% endblock %}
</body>
【问题讨论】:
标签: python django python-3.x django-templates django-views