[FIXED] Flask app is running correct function, but not rendering template


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:


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 = {}

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="", port=80)

The template is:

{% extends 'base.html' %}

{% block content %}

{user_data% from %likeing_user_list} {%endfor%}
# name User ID Profile URL Followers Verified status Block users
1 {{user_data['name']}} {{user_data['id']}} https://twitter.com/{{ user_data['username'] }} {{user_data['public_metrics']['followers_count']}} {{user_data['verified']}}
{% End Block %}

I don’t understand why the template doesn’t render when I post r.send . Any ideas?

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('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", "", true);
    r.onreadystatechange = function () {
        if (r.readyState != 4 || r.status != 200) return;
        //alert("Success: " + r.responseText);
        //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";


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)

Leave a Reply

(*) Required, Your email will not be published