首页 > 解决方案 > 不同的行不会出现在 Django 的上下文中

问题描述

如果我有类似的代码

hello_worlds = 'hello Earth \n hello Mars'

context = {
          'hellos':hello_worlds
          }

return render(request, 'home.html', context)

home.html 是

{{hellos}}

它显示为

hello Earth hello Mars

不是

hello Earth
hello Mars

我正在使用 Django 2.1.5 和 python 3.8 有没有办法让它像第二个一样显示?

标签: pythondjangopython-3.xdjango-views

解决方案


我想前两个家伙已经回答了。

您不需要在视图中换行,因为您可以使用“换行符”模板过滤器在模板中执行此操作,就像他们说的那样:

{{hellos|换行符}}

或者,

{{hellos|换行符}}


推荐阅读