2016-09-08 4 views
0

私はコンソール出力テキストをいくつか取りに行き、私のサイトのモーダルでdjango/jsを通してレンダリングしようとしています。コンソール出力を印刷するとき、改行はうまく動作しますが、サイト上でレンダリングすると、すべて1行で表示されます。私はすべて\ nを<br>に置き換えようとしましたが、何の効果もないようです。 <br>はプレーンテキストとして表示されます。これを行うためのより良い方法に関する考え方/なぜこれが最初に機能しないのか?私はHTMLではなくテキストを渡してきたはずです、stdoutストリームをhtmlに変換する(改行時に<br>を追加)

input_modal.find('.modal-body').text('Analysis complete'+response.console_output) 
+0

このスクリプトをどのように実行しますか? –

+0

このコードは入力を処理しません。なぜあなたはstdoutを置き換えていますか? Webサービスがある場合は、stdoutではなくhttp応答を処理してみてください。 –

+0

置き換え前後の行末には何がありますか? \ nを置換せずに検索するとヒットしますか?
の代わりに

を試しましたか? (最初に

を、最後に

を強制しなければならないでしょう)最後に、置き換え後に 'processing_std_out'で行う次のいくつかのことは何ですか? –

答えて

0

単純なミス:

import sys 
from io import StringIO 

# Save the old stdout 
old_stdout = sys.stdout 

# Save the stdout to variable 
sys.stdout = mystdout = StringIO() 

... # Do some processing that generates console text 

# Reset the to the old stdout 
sys.stdout = old_stdout 

# Get the stdout 
processing_std_out = mystdout.getvalue() 

# Replace all the linebreaks with <br> 
# This is the important part 
processing_std_out = processing_std_out.replace("\n","<br>") 

# return HttpResponse(json.dumps({'console_output':processing_std_out}), content_type="application/json") 

jsがこれです。また、テキストにプレタグを追加することは、すべて\ nを置換するよりも簡単です\

input_modal.find('.modal-body').html('Analysis complete'+response.console_output) 
関連する問題