【问题标题】:How I can show output in multi line in django? '\n' does not work如何在 django 中以多行显示输出? '\n' 不起作用
【发布时间】:2019-12-21 01:08:19
【问题描述】:

我正在 VSCode 中编写一个简单的 django 视图,我想在多行中编写我的 HttpRespose,但它不起作用?怎么了?

转义码都不起作用!

def index(request):
    return HttpResponse("firstline. \nanother line!!!!")

我期望输出

firstline.
another line!!!

但它的输出是

firstline. another line

【问题讨论】:

  • 使用
    return HttpResponse("firstline. <br/>another line!!!!").
  • 好吧,\n 实际上确实呈现了一个新行。但是浏览器只是忽略了这一点。但是,如果您返回“原始文本”流等,则会显示新行。

标签: python django vscode-debugger


【解决方案1】:

您向浏览器返回响应。 浏览器理解 html。 所以你需要返回“br”标签而不是/n 试试这个:

def index(request):
   return HttpResponse("firstline. <br/>another line!!!!")

【讨论】:

    【解决方案2】:

    如果您想输出纯文本,您可以通过添加正确的 mime 类型来实现。这样浏览器就不会做html格式化了。

    def index(request):
       return HttpResponse("firstline. \nanother line!!!!", content_type="text/plain") 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 2019-12-08
      相关资源
      最近更新 更多