【发布时间】:2015-02-01 18:22:08
【问题描述】:
我有一个简单的 JSON 数据:
{"thisdata": ["/home/fyp/Desktop/AVA/AVA-STORAGE", "Network has already been configured since nexpose-pc is in virtualbox! ...", "/home/fyp/Desktop/AVA/AVA-APP/RunGUI", "ubuntu-trusty\t192.168.0.21", "", "/home/fyp/Desktop/AVA/AVA-APP/RunGUI", "centos-7\t192.168.0.22", "", "/usr/sbin/apache2", "[sudo] password for fyp: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message", "(' * Restarting web server apache2\\n ...done.\\n', None)", "/usr/local/packer/packer", "/usr/bin/virtualbox", "centos 8 is not configured, going to install distribution", "Traceback (most recent call last):", "File \"../RunCMD/ava.py\", line 684, in <module>", "createvm.runPacker()", "File \"../RunCMD/ava.py\", line 124, in runPacker", "packer_tmp + '/' + self.Hostname + '_template.json')", "File \"/usr/lib/python2.7/shutil.py\", line 82, in copyfile", "with open(src, 'rb') as fsrc:", "IOError: [Errno 2] No such file or directory: 'TEMPLATES/redhat/template.json'"]}
我试图在我的 Flask 应用程序的模板中解析,但我似乎不知道该怎么做。
注意:不使用 jsonify() 我使用 json.dumps() 并将 JSON 发送到网页,我可以打印整个 JSON 数据块但不格式化。
这是我的主要应用:
@app.route('/', methods=['GET', 'POST'])
def home():
manual = HomeForm(request.form)
automatic = Automatic(request.form)
if request.method == 'POST':
if 'automatic' in request.form and automatic.validate():
run()
data_string = []
with open(logfile, 'r') as outfile:
for line in outfile:
data_string.append(line.strip())
json_string = jsonify({"thisdata": data_string})
print json_string
return render_template('index.html', form=manual, showouput='showouput', senddata=json_string)
flash('Please input all values!')
return render_template('index.html', form=manual)
如您所见,我正在尝试运行脚本,然后使用 JSON 格式在文件中返回一些行,如上所示。
但是我不知道如何解析 JSON,我感觉我没有以正确的方式发送 JSON,而且我不知道如何在 Jinja 模板中呈现 JSON。
我尝试在模板和其他建议中打印{{ senddata }},但没有显示任何内容。
【问题讨论】:
-
如果您要在模板中呈现 元素,为什么您认为需要将数据作为 JSON 格式?您的预期输出是什么?
-
@我有一个文件,我想在我的模板上以 div 形式显示其内容,如果您可以建议其他选项,将对我有很多帮助,这需要我提交上述数据和然后我只打印 div 表单中的内容谢谢
-
那么你就不需要 JSON 了。只需将您构建的列表传递给模板即可。
-
天哪,非常感谢我自己解决了,我不敢相信我花了几个小时来解决这个问题:)