现在我想将JSON数据发送到HTML,我只显示字典数据(JSON),如何在HTML中显示字典的值。 python代码是:
@app.route('/upload',methods=['get','post']) DeF上传(): if request.method='post': img_file=request.files['img_file'] 如果img_文件和允许的_文件(img_文件.filename): 文件名=安全文件名(img_file.filename) img_file.save(os.path.join(app.config['upload_folder'],filename))。 raw_img_url='./static/asset/uploads/'+文件名 结果img_url='./static/asset/output/line/'+文件名 比率=检查横幅(文件名) result_dict='result':比率 返回render_template(“upload.html”,result_img_url=result_img_url,result=json.dumps(result_dict))。 其他: 返回'''确定</p>'' 其他: 返回重定向(url_for('upload')) < /代码> HTML代码: %extends“base.html”% %块内容% <form method=“post”action=“/upload”enctype=“multipart/form data”> <input type=“file”id=“img_file”name=“img_file”class=“col-sm-4”> <input type=“submit”value=“check”class=“btn”> &表格/表格; & P & GT; %if result_img_url% <img src=“result_img_url”width='400'height='350'> {%Endif%} & lt;/p & gt; & UL & GT; <li>结果</li> &L/UL & GT; %端块%} < /代码> 但是结果html是,但是我只想显示结果的值: . @app.route('/upload', methods=['GET', 'POST']) def upload(): if request.method == 'POST': img_file = request.files['img_file'] if img_file and allowed_file(img_file.filename): filename = secure_filename(img_file.filename) img_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) raw_img_url = './static/asset/uploads/' + filename result_img_url = './static/asset/output/Line/' + filename ratio = check_banner(filename) result_dict = {'result': ratio} return render_template("upload.html", result_img_url=result_img_url, result=json.dumps(result_dict)) else: return ''' ok</p> ''' else: return redirect(url_for('upload')) HTML代码: {% extends "base.html" %} {% block content %} <form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" id="img_file" name="img_file" class="col-sm-4"> <input type="submit" value="check" class="btn"> </form> <p> {% if result_img_url %} <img src="{{ result_img_url }}" width='400' height='350'> {% endif %} </p> <ul> <li>{{result}}</li> </ul> {% endblock %} 但结果html是,但我只想显示结果的值:
HTML代码:
%extends“base.html”% %块内容% <form method=“post”action=“/upload”enctype=“multipart/form data”> <input type=“file”id=“img_file”name=“img_file”class=“col-sm-4”> <input type=“submit”value=“check”class=“btn”> &表格/表格; & P & GT; %if result_img_url% <img src=“result_img_url”width='400'height='350'> {%Endif%} & lt;/p & gt; & UL & GT; <li>结果</li> &L/UL & GT; %端块%} < /代码> 但是结果html是,但是我只想显示结果的值: . @app.route('/upload', methods=['GET', 'POST']) def upload(): if request.method == 'POST': img_file = request.files['img_file'] if img_file and allowed_file(img_file.filename): filename = secure_filename(img_file.filename) img_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) raw_img_url = './static/asset/uploads/' + filename result_img_url = './static/asset/output/Line/' + filename ratio = check_banner(filename) result_dict = {'result': ratio} return render_template("upload.html", result_img_url=result_img_url, result=json.dumps(result_dict)) else: return ''' ok</p> ''' else: return redirect(url_for('upload')) HTML代码: {% extends "base.html" %} {% block content %} <form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" id="img_file" name="img_file" class="col-sm-4"> <input type="submit" value="check" class="btn"> </form> <p> {% if result_img_url %} <img src="{{ result_img_url }}" width='400' height='350'> {% endif %} </p> <ul> <li>{{result}}</li> </ul> {% endblock %} 但结果html是,但我只想显示结果的值:
但是结果html是,但是我只想显示结果的值:
.
@app.route('/upload', methods=['GET', 'POST']) def upload(): if request.method == 'POST': img_file = request.files['img_file'] if img_file and allowed_file(img_file.filename): filename = secure_filename(img_file.filename) img_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) raw_img_url = './static/asset/uploads/' + filename result_img_url = './static/asset/output/Line/' + filename ratio = check_banner(filename) result_dict = {'result': ratio} return render_template("upload.html", result_img_url=result_img_url, result=json.dumps(result_dict)) else: return ''' ok</p> ''' else: return redirect(url_for('upload'))
{% extends "base.html" %} {% block content %} <form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" id="img_file" name="img_file" class="col-sm-4"> <input type="submit" value="check" class="btn"> </form> <p> {% if result_img_url %} <img src="{{ result_img_url }}" width='400' height='350'> {% endif %} </p> <ul> <li>{{result}}</li> </ul> {% endblock %}
但结果html是,但我只想显示结果的值:
如果您只想在 result>中传递一个参数,您可以更改 result dict=result':ratio. to result=ratio.>code>。
result>中传递一个参数,您可以更改 result dict=result':ratio. to result=ratio.>code>。
result dict=result':ratio. to result=ratio.>code>。
to result=ratio.>code>。
result=ratio.>code>。
它将只在页面中显示结果: . 它将只在页面中显示结果:
它将只在页面中显示结果: