Dec-30-2018, 07:36 PM
Summary and Background of What I'm Doing:
I'm creating a Flask quiz app which presents a user with three different options are allows them to input their answer using a form and submit button. I'm storing my key/values in a JSON file and I'm using ginja to load the data to the browser. Here's what the JSON file looks like:
[
{ "question": "What is the Capital of France?",
"option1":"Dublin",
"option2": "Paris",
"option3": "Cork",
"answer": "Paris",
"score": 0
},
{ "question": "What Was Freud's First Name",
"option1":"Fred",
"option2": "James",
"option3": "Sigmund",
"answer": "Sigmund",
"score": 0
}
]
My problem is related to how this data can be pass through several different pages using Flask. I can't seem to load or route this information to the browser and I don't really understand why. Here's my flask route:
Here's what I wrote in welcome.html:
{% for object in quiz %}
<br>
{{ quiz[1]["question"] }}
<br>
<br>
{{ quiz[1]["option1"] }}
<br>
{{ quiz[1]["option2"] }}
<br>
{{ quiz[1]["option3"] }}
<br>
<form action="/welcome" method='POST'>
Country:
<br>
<input type="text" name="welcome" value="">
<br>
<br>
<input type="submit" value="Submit">
</form>
{% endfor %}
Just in case...
If you need any more information or if I haven't been clear enough or explain this well, please let me know and I'll fix it.
Thank you.
I'm creating a Flask quiz app which presents a user with three different options are allows them to input their answer using a form and submit button. I'm storing my key/values in a JSON file and I'm using ginja to load the data to the browser. Here's what the JSON file looks like:
[
{ "question": "What is the Capital of France?",
"option1":"Dublin",
"option2": "Paris",
"option3": "Cork",
"answer": "Paris",
"score": 0
},
{ "question": "What Was Freud's First Name",
"option1":"Fred",
"option2": "James",
"option3": "Sigmund",
"answer": "Sigmund",
"score": 0
}
]
My problem is related to how this data can be pass through several different pages using Flask. I can't seem to load or route this information to the browser and I don't really understand why. Here's my flask route:
@app.route("/") @app.route('/question') def question(): data = [] with open("data/quiz.json", "r") as json_data: data = json.load(json_data) return render_template('question1.html', quiz=data) ###This is the route I use to load and read my JSON file before calling my first template titled 'question1.html'### @app.route('/capitals', methods=['POST']) def capitals(): answer = request.form["capitals"] if answer == "Paris": return render_template('welcome.html') else: return "<h4>is not correct, guess again.</h4>" ###This is my first template where I assign some simple logic to match the user's guess with the answer value. If the answer is correct, then the next question template is loaded### @app.route('/welcome', methods=['POST']) def welcome(): return render_template('welcome.html') ###And here's where the problem starts. The first page has the data loaded to the browser, but by the time I get to question two, the data doesn't appear anymore.###HTML Code:
Here's what I wrote in welcome.html:
{% for object in quiz %}
<br>
{{ quiz[1]["question"] }}
<br>
<br>
{{ quiz[1]["option1"] }}
<br>
{{ quiz[1]["option2"] }}
<br>
{{ quiz[1]["option3"] }}
<br>
<form action="/welcome" method='POST'>
Country:
<br>
<input type="text" name="welcome" value="">
<br>
<br>
<input type="submit" value="Submit">
</form>
{% endfor %}
Just in case...
If you need any more information or if I haven't been clear enough or explain this well, please let me know and I'll fix it.
Thank you.