Flask Message Flashing - get_flashed_messages()
In Flask, the get_flashed_messages() method is used to retrieve all the flash messages (from the session).
👇
<!-- flash messages --> {% for message in get_flashed_messages() %} <p>{{ message }}</p> {% endfor %}
Message Flashing
Flash messages are used to provide useful information to the user based on their actions with the app. The flash() method is used to create a flash message to be displayed in the next request.
👇
from flask import request, redirect, url_for, render_template, flash @stocks_blueprint.route('/add_stock', methods=['GET', 'POST']) def add_stock(): if request.method == 'POST': # ... save the data ... flash(f"Added new stock ({stock_data.stock_symbol})!") # <-- !! return redirect(url_for('stocks.list_stocks')) return render_template('stocks/add_stock.html')
Flask Redirect
In Flask, the redirect() function is used to redirect a user to a different URL.
redirect() can greatly improve the navigation through a site by automatically redirecting users to their expected pages.
👇
@app.route('/add_stock', methods=['GET', 'POST'])
def add_stock():
if request.method == 'POST':
# ... save the data ...
return redirect(url_for('list_stocks')) # <-- !!
return render_template('add_stock.html')
Comments (0)
Login to post a comment