{"id":1438,"date":"2023-08-20T19:14:40","date_gmt":"2023-08-20T13:44:40","guid":{"rendered":"https:\/\/geekpython.in\/?p=1438"},"modified":"2024-03-01T17:08:24","modified_gmt":"2024-03-01T11:38:24","slug":"flash-messages-on-frontend-using-flask","status":"publish","type":"post","link":"https:\/\/geekpython.in\/flash-messages-on-frontend-using-flask","title":{"rendered":"How to Flash Messages on Frontend using Flask"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Introduction<\/h1>\n\n\n\n<p>The Flask <code>flash()<\/code> function is an efficient way to display temporary messages to the user. This can be used to display a variety of messages, including error, notification, warning, and status messages.<\/p>\n\n\n\n<p>By the end of this article, you&#8217;ll be able to learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>How to use the <code>flash()<\/code> function<\/li>\n\n\n\n<li>Flashing messages on the frontend<\/li>\n\n\n\n<li>Flashing messages with categories<\/li>\n\n\n\n<li>Filtering flash messages based on categories<\/li>\n\n\n\n<li>Best practices for effectively using flashed messages<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Prerequisites<\/strong><\/h1>\n\n\n\n<p>Before you dive into this tutorial on flashing messages with Flask, make sure you have a fundamental understanding of the following concepts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Familiarity with Flask, including route handling, templates, and basic application structure.<\/li>\n\n\n\n<li>A basic understanding of HTML markup is necessary, as you&#8217;ll be working with HTML templates to render the flashed messages on the frontend.<\/li>\n\n\n\n<li>Understanding of Jinja2 templating engine, which is integrated with Flask for rendering dynamic content within HTML templates.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>How to Use<\/strong> flash() Method<\/h1>\n\n\n\n<p>As previously mentioned, the <code>flash()<\/code> function is used to display messages to the users after the specific request is made. Its primary goal is to offer relevant feedback, which enhances the user experience.<\/p>\n\n\n\n<p>The <code>flash()<\/code> function accepts two parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>message<\/code>: The message to display to the user.<\/li>\n\n\n\n<li><code>category<\/code>: Specifies the message category. This is an optional parameter.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Using flash() within Flask App<\/h2>\n\n\n\n<p>This section describes how to integrate the message flashing functionality into the Flask application and display it on the frontend.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importing flash and other required modules<\/h3>\n\n\n\n<p>Create an <code>app.py<\/code> file and import the <code>flash<\/code> from <code>flask<\/code>, along with other necessary modules, into the file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:2 decode:true \" title=\"app.py\" >from flask import Flask, render_template, request, url_for, redirect\nfrom flask import flash<\/pre><\/div>\n\n\n\n<p>You could also import these modules in a single line as well.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Flask App Setup<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"app.py\" >app = Flask(__name__)\napp.secret_key = \"Sachin\"<\/pre><\/div>\n\n\n\n<p>An instance of <code>Flask<\/code> is created and stored within the <code>app<\/code> variable to initialize the Flask application.<\/p>\n\n\n\n<p>The Flask application&#8217;s secret key is assigned as <code>\"Sachin\"<\/code> using the <code>secret_key<\/code> attribute of the <code>app<\/code> object. This key will serve for session management.<\/p>\n\n\n\n<p>Note: It&#8217;s essential to store the secret key in a secure environment, rather than hardcoding it directly within the application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Implementing flash() Function within View Functions<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:8,11 decode:true \" title=\"app.py\" >@app.route(\"\/info\", methods=[\"GET\", \"POST\"])\ndef add_info():\n    if request.method == \"POST\":\n        name = request.form['name']\n        profession = request.form['profession']\n\n        if name == \"\" or profession == \"\":\n            flash(\"Invalid: Every field is required.\")\n            return redirect(url_for(\"add_info\"))\n        else:\n            flash(\"Success: Info added successfully.\")\n            return redirect(url_for(\"add_info\"))\n    return render_template(\"info.html\")<\/pre><\/div>\n\n\n\n<p>The route <code>\"\/info\"<\/code> is created to manage both <code>GET<\/code> and <code>POST<\/code> requests, and it is linked to the <code>add_info()<\/code> view function.<\/p>\n\n\n\n<p>Within the <code>add_info()<\/code> view function, it checks whether the request is a <code>POST<\/code> request, and then retrieves the values from the <code>\"Name\"<\/code> and <code>\"Profession\"<\/code> form fields.<\/p>\n\n\n\n<p>Following that, it examines if any of the fields are empty. If they are, a message <code>\"Invalid: Every field is required.\"<\/code> is displayed using the <code>flash()<\/code> function, and the route remains the same. Conversely, if none of the fields are empty, a message <code>\"Success: Info added successfully.\"<\/code> is flashed using the <code>flash()<\/code> function, and the route continues for further information input.<\/p>\n\n\n\n<p>However, this won&#8217;t immediately display the message on the frontend. To achieve that, you must use the <code>get_flashed_messages()<\/code> function within the template.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Implementing get_flashed_messages() Function within Message Template<\/h3>\n\n\n\n<p>Create a file named <code>message.html<\/code> within the <strong>templates<\/strong> directory in your project.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml mark:1 decode:true \" title=\"message.html\" >{% with msg = get_flashed_messages() %}\n{% if msg %}\n{% for message in msg %}\n&lt;div class=\"alert alert-warning alert-dismissible fade show\" role=\"alert\"&gt;\n  &lt;strong&gt;{{ message }}&lt;\/strong&gt;\n  &lt;button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\" aria-label=\"Close\"&gt;&lt;\/button&gt;\n&lt;\/div&gt;\n{% endfor %}\n{% endif %}\n{% endwith %}<\/pre><\/div>\n\n\n\n<p>The <code>get_flashed_messages()<\/code> function retrieves the flashed messages and preserves them in the session. The function is invoked using the <code>with<\/code> statement and its output is stored in the <code>msg<\/code> variable.<\/p>\n\n\n\n<p>The <code>{% if msg %}<\/code> statement evaluates whether any flashed messages are stored in the <code>msg<\/code> variable. If such messages exist, the <code>{% for message in msg %}<\/code> block iterates through each message and renders them using <code>{{ message }}<\/code> within the Bootstrap alert component.<\/p>\n\n\n\n<p>Ultimately, the loop terminated using <code>{% endfor %}<\/code>, the conditional block is terminated using <code>{% endif %}<\/code>, and the <code>with<\/code> statement block is terminated with <code>{% endwith %}<\/code>.<\/p>\n\n\n\n<p>Now you can include the message template in any of your templates to display the messages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Preparing the Frontend<\/h3>\n\n\n\n<p>Create two HTML files in the <code>templates<\/code> folder with the names <code>base.html<\/code> and <code>info.html<\/code>.<\/p>\n\n\n\n<p><strong>base.html<\/strong><\/p>\n\n\n\n<p>This file will contain the basic HTML layout with Bootstrap CSS and JavaScript.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" title=\"base.html\" >&lt;!doctype html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;meta content=\"width=device-width, initial-scale=1\" name=\"viewport\"&gt;\n\n    &lt;link crossorigin=\"anonymous\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.1.3\/dist\/css\/bootstrap.min.css\"\n          integrity=\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\" rel=\"stylesheet\"&gt;\n\n    &lt;title&gt;Flash Message Using Flask&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n{% block content %} {% endblock %}\n\n&lt;script crossorigin=\"anonymous\"\n        integrity=\"sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p\"\n        src=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.1.3\/dist\/js\/bootstrap.bundle.min.js\"&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p><strong>info.html<\/strong><\/p>\n\n\n\n<p>This file will hold a form for inputting information and includes the <code>message.html<\/code> template to display flashed messages.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml mark:4 decode:true \" title=\"info.html\" >{% extends 'base.html' %}\n\n{% block content %}\n{% include 'message.html' %}\n&lt;div class=\"container my-5\"&gt;\n    &lt;form action=\"\/info\" method=\"post\"&gt;\n        &lt;div class=\"mb-3\"&gt;\n            &lt;label class=\"form-label\" for=\"name\"&gt;Name&lt;\/label&gt;\n            &lt;input class=\"form-control\" id=\"name\" name=\"name\" type=\"text\"&gt;\n        &lt;\/div&gt;\n        &lt;div class=\"mb-3\"&gt;\n            &lt;label class=\"form-label\" for=\"profession\"&gt;Profession&lt;\/label&gt;\n            &lt;input class=\"form-control\" id=\"profession\" name=\"profession\" type=\"text\"&gt;\n        &lt;\/div&gt;\n        &lt;button class=\"btn btn-dark\" type=\"submit\"&gt;Submit&lt;\/button&gt;\n    &lt;\/form&gt;\n&lt;\/div&gt;\n{% endblock %}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Running the Flask Application<\/h3>\n\n\n\n<p>When you execute the <code>app.py<\/code> file and evaluate the frontend, you&#8217;ll observe flashed messages upon making requests.<\/p>\n\n\n\n<p>Here&#8217;s a preview of the application hosted on the <code>127.0.0.1:5000<\/code> server using the <code>\"\/info\"<\/code> route.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1366\" height=\"709\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-7.png\" alt=\"Flask Webpage preview\" class=\"wp-image-1445\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-7.png 1366w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-7-300x156.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-7-1024x531.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-7-768x399.png 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><figcaption class=\"wp-element-caption\">Flask Webpage preview<\/figcaption><\/figure>\n\n\n\n<p>When submitting the form without completing the <strong>&#8220;Profession&#8221;<\/strong> field, the application displays the relevant flashed message.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1338\" height=\"706\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-6.png\" alt=\"Invalid message displayed\" class=\"wp-image-1444\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-6.png 1338w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-6-300x158.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-6-1024x540.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-6-768x405.png 768w\" sizes=\"auto, (max-width: 1338px) 100vw, 1338px\" \/><figcaption class=\"wp-element-caption\">Invalid message displayed<\/figcaption><\/figure>\n\n\n\n<p>When the form is submitted with all fields completed, the relevant message is displayed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1338\" height=\"697\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-4.png\" alt=\"Success message displayed\" class=\"wp-image-1442\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-4.png 1338w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-4-300x156.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-4-1024x533.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-4-768x400.png 768w\" sizes=\"auto, (max-width: 1338px) 100vw, 1338px\" \/><figcaption class=\"wp-element-caption\">Success message displayed<\/figcaption><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Flashing Messages with Categories<\/strong><\/h1>\n\n\n\n<p>Each message serves a unique purpose. Some aim to caution users, while others indicate successful task completion. Assigning categories to the messages makes them distinct from one another and provides a better user experience.<\/p>\n\n\n\n<p>The <code>get_flashed_messages()<\/code> function offers a <code>with_categories<\/code> parameter that can be set to <code>True<\/code>. Doing so enables you to utilize the category assigned to the message in the <code>flash()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Assigning Categories to the Messages<\/h3>\n\n\n\n<p>To assign categories, make use of the <code>flash()<\/code> function. Adjust your <code>add_info()<\/code> view function in the <code>app.py<\/code> file as demonstrated in the following code:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:8,11 decode:true \" title=\"app.py\" >@app.route(\"\/info\", methods=[\"GET\", \"POST\"])\ndef add_info():\n    if request.method == \"POST\":\n        name = request.form['name']\n        profession = request.form['profession']\n\n        if name == \"\" or profession == \"\":\n            flash(\"Invalid: Every field is required.\", \"danger\")\n            return redirect(url_for(\"add_info\"))\n        else:\n            flash(\"Success: Info added successfully.\", \"success\")\n            return redirect(url_for(\"add_info\"))\n    return render_template(\"info.html\")<\/pre><\/div>\n\n\n\n<p>The categories <code>danger<\/code> and <code>success<\/code> are assigned to both messages. These categories correspond to Bootstrap CSS classes. Specifically, <strong>&#8220;danger&#8221;<\/strong> signifies errors, while <strong>&#8220;success&#8221;<\/strong> signifies successful completion.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Retrieving Categories and Applying Styles<\/h3>\n\n\n\n<p>To utilize categories from flashed messages in templates, retrieve them using <code>get_flashed_messages(with_category=True)<\/code> within the template. Then, iterate through them in a similar manner as you did with regular messages.<\/p>\n\n\n\n<p>Adjust your <code>message.html<\/code> template by implementing the provided code as provided below:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml mark:1,3-4 decode:true \" title=\"message.html\" >{% with msg = get_flashed_messages(with_categories=True) %}\n{% if msg %}\n{% for category, message in msg %}\n&lt;div class=\"alert alert-{{ category }} alert-dismissible fade show\" role=\"alert\"&gt;\n  &lt;strong&gt;{{ message }}&lt;\/strong&gt;\n  &lt;button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\" aria-label=\"Close\"&gt;&lt;\/button&gt;\n&lt;\/div&gt;\n{% endfor %}\n{% endif %}\n{% endwith %}<\/pre><\/div>\n\n\n\n<p>The categories are retrieved and utilized to apply specific CSS properties to the Bootstrap alert component, tailored to the respective request.<\/p>\n\n\n\n<p>With these changes in place, run the application again. You&#8217;ll observe messages being highlighted in varying colors that correspond to the specific request.<\/p>\n\n\n\n<p>The illustration below depicts how the message&#8217;s appearance differs between invalid and successful form submissions, respectively.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1342\" height=\"697\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-3.png\" alt=\"Invalid message highlighted with red\" class=\"wp-image-1441\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-3.png 1342w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-3-300x156.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-3-1024x532.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-3-768x399.png 768w\" sizes=\"auto, (max-width: 1342px) 100vw, 1342px\" \/><figcaption class=\"wp-element-caption\">Invalid message highlighted with red<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1344\" height=\"697\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2.png\" alt=\"Success message highlighted with green\" class=\"wp-image-1440\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2.png 1344w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2-300x156.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2-1024x531.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2-768x398.png 768w\" sizes=\"auto, (max-width: 1344px) 100vw, 1344px\" \/><figcaption class=\"wp-element-caption\">Success message highlighted with green<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Another Use Case of Categories<\/h3>\n\n\n\n<p>Categories could also serve as prefixes to messages, offering another practical application. Insert the provided attribute inside the <code>&lt;div><\/code> block of the Bootstrap alert component.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" title=\"message.html\" >&lt;strong&gt;{{ category }} - {{ message }}&lt;\/strong&gt;<\/pre><\/div>\n\n\n\n<p>The provided code will add the corresponding category as a prefix to the actual message. This will result in an appearance similar to what&#8217;s displayed in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1347\" height=\"697\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1.png\" alt=\"Used category as a prefix\" class=\"wp-image-1439\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1.png 1347w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1-300x155.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1-1024x530.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1-768x397.png 768w\" sizes=\"auto, (max-width: 1347px) 100vw, 1347px\" \/><figcaption class=\"wp-element-caption\">Used category as a prefix<\/figcaption><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Filtering Flash Messages Based on Categories<\/strong><\/h1>\n\n\n\n<p>To filter messages based on categories, you can utilize the <code>category_filter<\/code> parameter within the <code>get_flashed_messages()<\/code> function in the template. This parameter accepts a list of message categories.<\/p>\n\n\n\n<p>Create a new file named <code>filtered_message.html<\/code> within the templates folder. Then, insert the provided code snippet into this file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml mark:1 decode:true \" title=\"filtered_message.html\" >{% with error_msg = get_flashed_messages(category_filter=[\"danger\"]) %}\n{% if error_msg %}\n{% for message in error_msg %}\n&lt;div class=\"alert alert-danger alert-dismissible fade show\" role=\"alert\"&gt;\n  &lt;strong&gt;{{ message }}&lt;\/strong&gt;\n  &lt;button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\" aria-label=\"Close\"&gt;&lt;\/button&gt;\n&lt;\/div&gt;\n{% endfor %}\n{% endif %}\n{% endwith %}<\/pre><\/div>\n\n\n\n<p>The code snippet mentioned above retrieves and filters messages based on the <strong>&#8220;danger&#8221;<\/strong> category using <code>get_flashed_messages(category_filter=[\"danger\"])<\/code>. If there are flashed messages matching this category, the code iterates through them and displays them when the relevant request is made.<\/p>\n\n\n\n<p>To display only messages with the <strong>&#8220;danger&#8221;<\/strong> category on the frontend, include the <code>filtered_message.html<\/code> template within <code>info.html<\/code>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Best Practices for Using Flashed Messages in Flask<\/strong><\/h1>\n\n\n\n<p>Flashed messages are highly valuable for enhancing the user experience through prompt feedback. To maximize their impact, it&#8217;s important to implement the following practices.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Provide clear and concise messages, and avoid displaying messages on every request to prevent overwhelming users.<\/li>\n\n\n\n<li>When presenting error messages, include instructions on how users can address the issue.<\/li>\n\n\n\n<li>Assign suitable categories to messages that accurately reflect their content and purpose.<\/li>\n\n\n\n<li>As flashed messages are temporary, consider adding a dismissal button to allow users to remove them once they are no longer needed.<\/li>\n\n\n\n<li>Refrain from revealing any sensitive details in messages, as this could potentially compromise the security of your application.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Conclusion<\/h1>\n\n\n\n<p>Flashing messages on an application helps to notify users about some action performed, be it task completion, error, or warning. It enhances the user experience but only if it is used keeping in mind the best practices.<\/p>\n\n\n\n<p>In this article, the secret key is hardcoded in the application, which is not a good practice. Store sensitive details like secret keys within the protected environment and refrain from revealing them in messages.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/structure-flask-app-with-blueprint\">How to structure Flask app using Blueprint<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/render-images-from-flask\">Upload and display images on the frontend using Flask in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/flask-app-for-image-recognition\">Building a Flask image recognition webapp using a deep learning model<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app\">How to connect the SQLite database with Flask app using Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python\">How to create a database in Appwrite using Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/merging-tailwindcss-into-flask-apps\">How to Integrate TailwindCSS with Flask<\/a>?<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The Flask flash() function is an efficient way to display temporary messages to the user. This can be used to display a variety of messages, including error, notification, warning, and status messages. By the end of this article, you&#8217;ll be able to learn: Prerequisites Before you dive into this tutorial on flashing messages with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1448,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[5,2],"tags":[12],"class_list":["post-1438","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flask","category-python","tag-python","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1438"}],"version-history":[{"count":4,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1438\/revisions"}],"predecessor-version":[{"id":1450,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1438\/revisions\/1450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1448"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}