질문
Flask의 HTML 페이지에 문자열을 전달하기 전에 문자열을 수정하려고했지만 ( '\ n'을 '< br >'으로 대체) 사용하는 일반적인 방법이 작동하지 않습니다. 몇 가지 이유.
finalstring = textstring.replace('\n', '<br>')
return render_template('my-form-result.html', emailresponse = finalstring)
이것은 작동하지만 어떤 이유로 인해 아무것도 대체되지 않습니다. 이 작업을 수행하려면 어떻게해야합니까? 감사!
답변1
면책 조항 : 저는 Flask와 함께 일한 적이 없습니다. 그냥 찾아보고 원하는대로 작동하기를 바랍니다.
따라서 템플릿 my-form-result.html
어딘가에 다음을 포함하는 줄이 있습니다.
{{ emailresponse }}
이것을 다음으로 바꿀 수 있습니다.
{% for line in emailresponse.split('\n') %}
{{ line }}
<br />
{% endfor %}
모든 줄 바꿈 뒤에 br
을 추가하려면
답변2
replace ()
코드가 정확합니다. 템플릿에서 HTML을 이스케이프해야합니다.
{{ emailresponse|safe }}
진단하려면 다음을 시도하십시오.
finalstring = textstring.replace('\n', '<br>')
print(finalstring)
return render_template('my-form-result.html', emailresponse = finalstring)
또한 웹페이지의 소스 코드를 보여 주어 템플릿에서 실제로 렌더링되는 내용을 확인하십시오.
출처 : https://stackoverflow.com/questions/63024007/how-to-replace-n-in-string-with-br-in-flask-app