我編寫了一段匹配的代碼,如果該字符串具有模式文本中的任何內(nèi)容。someExtension,就我而言,它將是字符串中的fileName.png,將其轉(zhuǎn)換為img標(biāo)簽,并使用python和在HTML文件上顯示燒瓶。讓我們以示例字符串為例:“此程序的輸出是什么?e.png”,代碼匹配e.png,然后將其替換為"<br><img src="{{url_for('static', filename='pics/e.png')}}" /><br>"圖像e.png放在靜態(tài)文件夾內(nèi)的文件夾pics中。如果即使通過向其添加Markup()將該字符串推入flask變量中,它也不會呈現(xiàn)圖像,而是顯示以下輸出。為什么會這樣呢?有什么辦法讓它顯示圖像e.png?我的代碼是:import refrom flask import Flask, Markup, render_templateapp = Flask(__name__)def rep(x): text = re.findall(r'\w+[.]\w+', x) for i in text: b ="<img src=\"{{ url_for('static',filename='pics/"+i+"')}}\">" x=x.replace(i,b) return x@app.route('/')def home(): a = "What is the output of this program? e.png" a = rep(a) return render_template('new.html',var=Markup(a))if __name__ == '__main__': app.run(debug=True, host='localhost', port=8032)HTML文件是<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Title</title></head><body> {{var}}</body></html>
添加回答
舉報(bào)
0/150
提交
取消