'Python - Flash messaging not working in flask app [duplicate]

This is my app.py file

import pymysql
from flask import Flask, flash, render_template, request, redirect, url_for
import sqlalchemy as db
import pymysql
import mysql.connector
import json


app = Flask(__name__)

@app.route('/thankyou')
def thanks():
    return render_template('thankyou.html')

@app.route('/register', methods=['POST'])
def save_user_info():

    conn = mysql.connector.connect(
    host="newdatabase",
    user="user",
    password="password",
    database="users",
    port=3306
    )

    cursor = None
    try:
        name = request.form['name']
        dob = request.form['dob']
        gender = request.form['gender']
        password = request.form['password']
        phone = request.form['phone']
        email = request.form['email']
        address = request.form['address']

        # validate the received values
        if name and dob and gender and password and phone and email and address and request.method == 'POST':

            # do not save password as a plain text
            #_hashed_password = generate_password_hash(password)

            # save user information
            sql = "INSERT INTO user(name, password, email, phone, gender, dob, address) VALUES(%s, %s, %s, %s, %s, %s, %s)"
            data = (name, password, email, phone, gender, dob, address)
            cursor = conn.cursor()
            cursor.execute(sql, data)
            conn.commit()

            flash('Hello'+name)
            return redirect(url_for('thanks'))
            #return 'thanks'
        else:
            return 'Error while saving user information'
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        conn.close()


@app.route('/')
def home():
    return render_template('multi-step-registration.html')


if __name__ == "__main__":
    app.run()


And, this is the 'thankyou.html' file where the flash messages are being printed.

<!doctype html>
<html>
   <head>
      <title>Registration Successful!</title>
   </head>
   <body>
      {% with messages = get_flashed_messages() %}
         {% if messages %}
            <ul>
               {% for message in messages %}
               <li<{{ message }}</li>
               {% endfor %}
            </ul>
         {% endif %}
      {% endwith %}
        
      <h1>User Registered</h1>
   </body>
</html>

This is the error I get after submitting the form via the 'save_user_info()' function in the app.

 ERROR in app: Exception on /register [POST]

Traceback (most recent call last):

  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app

    response = self.full_dispatch_request()

  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1519, in full_dispatch_request

    return self.finalize_request(rv)

  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1538, in finalize_request

    response = self.make_response(rv)

  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1701, in make_response

    raise TypeError(

TypeError: The view function for 'save_user_info' did not return a valid response. The function either returned None or ended without a return statement.

This occurs only when I use flash messages and redirect them to a html page. If I simply return from the function through 'return 'ok' ' or anything, the app works fine.

Could someone tell me why this flash messaging isnt working? I am not sure what is wrong with my code.



Solution 1:[1]

If this is only occurring when you flash a message, it appears that your 'li' tag is formatted incorrectly when you add the messages to your HTML.

<li<{{ message }}</li>

Notice your backwards < after the opening 'li' tag.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Benjamin McKay