简体   繁体   中英

Not able to parse JSON object in Flask Jinja template

I have a simple JSON data:

{"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'"]}

Which I am trying to parse in the template of my Flask app, however I don't seem to know how to do that.

Note: without using the jsonify() I use json.dumps() and sent the JSON to the webpage I could print the whole block of JSON data but not formatted.

This is my main app:

@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)

As you can see I am trying to run a script and then return some lines in a file using JSON format as shown above.

However I do not know how to parse the JSON, I have a feeling I am not sending the JSON in a proper way and also I do not know how to render the JSON in the Jinja template.

I tried printing {{ senddata }} in the template and other suggestions but it does not show anything.

All i had to do was just parse the list instead of json since my main motive is to display the file content in div form. and in the template i just render the list :

            data_string = []
        with open(logfile, 'r') as outfile:
            for line in outfile:
                data_string.append(line.strip())

        return render_template('index.html', form=manual, showouput='showouput', senddata=data_string)


{% if senddata %}
{% for n in senddata%}
    <div>{{ n }}</div>
{%endfor%}
{% endif %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM