问题描述
我开发了一个django项目。
我想在html上显示输出字符串。
我的看法是:
def strategy_run(request):
output = "hello world!\nstring test"
return render_to_response('strategy_run.html', locals())
我的strategy_run.html是:
<!DOCTYPE html>
<html lang="en">
<body>
{{output}}
</body>
</html>
但我希望html显示如下:
hello world!
string test
他们的html可能是"hello world!<br>string test"
。
那么如何将字符串更改为html格式?
是否有用于更改格式的python或django函数?
谢谢。
1楼
我不确定您要问的是什么,但是听起来您可能想要linebreaks
或linebreaksbr
。
2楼
多种方法可以做到这一点。
from django.http import HttpResponse
def strategy_run(request):
return HttpResponse("hello world!<br>string test")
第2
def strategy_run(request):
output = "hello world!<br>string test"
return render_to_response('strategy_run.html', locals())
仅在您的stragegy_run.html中排在第三
<!DOCTYPE html>
<html lang="en">
<body>
{{ output|linebreaksbr }}
</body>
</html>