Issue
I have a flask app with a template. I’m trying to send some items collected by javascript via post when the user clicks a button. When I post the data to /confirm the print statement executes, but the render_template function doesn’t render anything and the web browser stays on the /likes page.
web_interface.py is:
#!/bin/python3
Import from Flask Flask, render_template, request, url_for, redirect
import twitter as twitter
import data class from data class
import json
app = Flask(__name__)
form-data = {}
tweet_data = {}
@app.route("/")
Def index():
return render_template("index.html")
@app.route("/likes", methods=['POST','GET'])
def nice():
If request.method == 'POST':
tweet_url = request.form['inputTweetURL1']
Favorite user list = twitter.Favorite user lookup_by_url(tweet_url)
return render_template("likes.html", Favorite User List = Favorite User List)
other than that:
return render_template("likes.html")
@app.route('/confirm', methods=['POST'])
Check differential ():
#block_list = json.loads(request.data.decode())["data"]
# print("Print data: {}".format(foo))
block_list = json.loads(request.data.decode())['data']
print(block list)
return render_template("confirm.html", block_list=block_list)
app.run(host="0.0.0.0", port=80)
The template is:
{% extends 'base.html' %}
{% block content %}
#
name
User ID
Profile URL
Followers
Verified status
Block users
{user_data% from %likeing_user_list}
1
{{user_data['name']}}
{{user_data['id']}}
https://twitter.com/{{ user_data['username']
}}
{{user_data['public_metrics']['followers_count']}}
{{user_data['verified']}}
{%endfor%}
{% End Block %}
I don’t understand why the template doesn’t render when I post r.send . Any ideas?
Update:
Here’s the updated routing:
@app.route('/confirm', methods=['POST','GET'])
Check differential ():
If request.method == 'POST':
#block_list = json.loads(request.data.decode())["data"]
# print("Print data: {}".format(foo))
block_list = json.loads(request.data.decode())['data']
#print(block_list)
print('Your post worked')
return render_template("confirm.html", block_list=block_list)
other than that:
block_list = json.loads(request.data.decode())['data']
print('you're gettin')
return render_template('confirm.html', block_list=block_list)
The function to which the POST request is sent is: Should I do a redirect here?
var r = new XMLHttpRequest();
r.open("POST", "http://127.0.0.1/verify", true);
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
//alert("Success: " + r.responseText);
console.log("sent");
//window.location.href = "/confirm"; // Redirect to /confirm
};
// send data from JS in the following way
r.send(JSON.stringify({ "data": dataToSend }));
window.location.href = "/verify";
Solution
You can simplify the logic by sending the ids of the selected rows to the /confirm
endpoint. Then let the endpoint figure our what data the ids represent and render it for you.
@app.route('/confirm', methods=['GET'])
def confirm():
args = request.args
ids = args.get('ids')
id_list = ids.split(',')
return render_template('confirm.html', id_list=id_list)
Answered By – Emiel Zuurbier
Answer Checked By – Robin (Easybugfix Admin)