Re: [問題] 在客戶端的網頁顯示

看板Python作者 (非常念舊)時間9年前 (2015/01/17 14:00), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串2/2 (看更多)
這是我的程式碼... 主程式: from flask import Flask,url_for,send_from_directory,render_template app=Flask(__name__) from filter import nl2br app.jinja_env.filters['nl2br']=nl2br @app.route("/") def hello1(): import subprocess cmd=subprocess.Popen(['ifconfig'],stdout=subprocess.PIPE,stderr=subprocess.PIPE) out=cmd.communicate() templateData={ 'result' : out } return render_template('test.html',**templateData) if __name__=="__main__": app.run(host='192.168.10.128',port=1300,debug=True) ============================================================================== filter.py: #!/usr/bin/env python import re from jinja2 import evalcontextfilter, Markup, escape _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}') @evalcontextfilter def nl2br(eval_ctx, value): result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n') for p in _paragraph_re.split(escape(value))) if eval_ctx.autoescape: result = Markup(result) return result ============================================================================== test.html: <h1>{{ result | nl2br }}</h1> 顯示還是一樣....謝謝。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.136.158.100 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1421474418.A.F29.html

01/17 17:34, , 1F
你要不要貼一下你 render 完頁面的原始碼…
01/17 17:34, 1F

01/17 17:38, , 2F

01/17 17:39, , 3F
'<br>\n' 改成 Markup('<br/>\n') 看看吧
01/17 17:39, 3F

01/19 11:23, , 4F
cc大,用您的建議,她可以顯示出來了,謝謝。
01/19 11:23, 4F
文章代碼(AID): #1KkVfoyf (Python)
文章代碼(AID): #1KkVfoyf (Python)