{"id":8619,"date":"2020-09-18T11:07:04","date_gmt":"2020-09-18T11:07:04","guid":{"rendered":"https:\/\/www.askpython.com\/?p=8619"},"modified":"2020-09-18T11:07:05","modified_gmt":"2020-09-18T11:07:05","slug":"flask-flash-method","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/flask\/flask-flash-method","title":{"rendered":"Flask flash() method &#8211; How to Flash Messages in Flask?"},"content":{"rendered":"\n<p>In this tutorial, we will learn how to flash messages using the Flask flash() method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What does it mean to flash a message?<\/strong><\/h2>\n\n\n\n<p>It is always good for GUI applications to give feedback to the user for his actions. <\/p>\n\n\n\n<p>That is, for example, in <a href=\"https:\/\/www.askpython.com\/python-modules\/flask\/flask-forms\" class=\"rank-math-link\">Flask form<\/a>, if the user leaves a field empty, it is appreciable to give him an error\/info message telling that the field is required.<\/p>\n\n\n\n<p>The <a href=\"https:\/\/www.askpython.com\/python\/tkinter-gui-widgets\" class=\"rank-math-link\">Python Tkinter GUI<\/a> framework uses the <strong><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-messagebox-and-radiobutton\" class=\"rank-math-link\">message<\/a><\/strong> or the<strong> dialogue box <\/strong>to show the message<strong>,<\/strong> and in Client-Side Scripting Language JavaScript, it is done using the <strong>alert<\/strong> function.<\/p>\n\n\n\n<p>And in Flask, we have this flash method to do precisely that.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How does Flask flash work?<\/strong><\/h2>\n\n\n\n<p>The Flask flash method shows messages to the users. <\/p>\n\n\n\n<p>With Flash, we can create a flash message in one Flask View and then show it in another View, called \u201cnext,\u201d which usually is a template View.<\/p>\n\n\n\n<p>A typical example of <a href=\"https:\/\/www.askpython.com\/python-modules\/flask\/flask-templates\" class=\"rank-math-link\">Template<\/a> View is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n@app.route(&#039;\/template&#039;)\ndef blog():\n    #codes...\n    #codes...\n    return render_template(&#039;template.html&#039;)\n<\/pre><\/div>\n\n\n<p>Hence, a Flask view creates a Flash message in one view and then passes it to the <strong>next<\/strong> view( along with the request), which displays the message to the user.<\/p>\n\n\n\n<p>The syntax for Flash:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nflash(message,category)\n<\/pre><\/div>\n\n\n<p>Here,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>message:<\/strong> The message to display<\/li><li><strong>category:<\/strong> An Optional parameter, which can be set to &#8220;error,&#8221; &#8220;info,&#8221; or &#8220;warning.&#8221;<\/li><\/ul>\n\n\n\n<p>To extract the flash message from the session, where it is stored, and display it on the template, we use the <strong>get_flashed_messages()<\/strong> function. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nget_flashed_messages(with_categories, category_filter)\n<\/pre><\/div>\n\n\n<p>Here,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>with_categories:<\/strong> An Optional Parameter tuple to mention the category(error\/info\/warning)<\/li><li><strong>category_filter:<\/strong> An Optional Parameter to filter and display only specific messages<\/li><\/ul>\n\n\n\n<p>A simple example showing the <strong>get_flashed_message() <\/strong>in Template file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{% with messages = get_flashed_messages() %}\n   {% if messages %}\n      {% for message in messages %}\n         {{ message }}\n      {% endfor %}\n   {% endif %}\n{% endwith %}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Hands-On with Flask flash() method<\/strong><\/h2>\n\n\n\n<p>Here we will create a simple Flask application that flashes &#8211;<strong> login successful<\/strong> if the user enters the correct password.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Coding the Flask App<\/strong>lication File<\/h3>\n\n\n\n<p>Here we will show a simple form taking in a password. If the password is correct then flash the message.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom flask import Flask,render_template,request,redirect,flash\n\napp = Flask(__name__)\n\n@app.route(&#039;\/form&#039;)\ndef form():\n    return render_template(&#039;form.html&#039;)\n\n@app.route(&#039;\/login&#039;, methods = &#x5B;&#039;POST&#039;, &#039;GET&#039;])\ndef login():\n    if request.method == &#039;GET&#039;:\n        return &quot;Login via the login Form&quot;\n    \n    if request.method == &#039;POST&#039;:\n        password = request.form&#x5B;&#039;password&#039;]\n        if password == &#039;123&#039;:\n            #The following flash message will be displayed on successful login\n            flash(&#039;Login successful&#039;)\n            return render_template(&#039;success.html&#039;)\n        else:\n            return redirect(&#039;\/form&#039;)\n\napp.run(host=&#039;localhost&#039;, port=5000)\n<\/pre><\/div>\n\n\n<p>Here the <strong>success.html<\/strong> is the &#8220;next&#8221; Template since the message will appear there.<\/p>\n\n\n\n<p>That&#8217;s it !! let us now code the Templates <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Coding the Templates<\/strong><\/h3>\n\n\n\n<p>The form.html will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;form action=&quot;\/login&quot; method = &quot;POST&quot;&gt;\n   &lt;p&gt;password &lt;input type = &quot;text&quot; name = &quot;password&quot; \/&gt;&lt;\/p&gt;\n   &lt;p&gt;&lt;input type = &quot;submit&quot; value = &quot;Submit&quot; \/&gt;&lt;\/p&gt;\n&lt;\/form&gt;\n<\/pre><\/div>\n\n\n<p>And the Success.html template file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{% with messages = get_flashed_messages() %}\n   {% if messages %}\n      {% for message in messages %}\n         {{ message }}\n      {% endfor %}\n   {% endif %}\n{% endwith %}\n\n&lt;h2&gt;User Authenticated&lt;\/h2&gt;\n<\/pre><\/div>\n\n\n<p>Notice how we used the <strong>get_flashed_messages()<\/strong> here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Implementation<\/strong><\/h3>\n\n\n\n<p>That&#8217;s it !! Let us now fire up the server and check our webpage<\/p>\n\n\n\n<p>Hit <strong>&#8220;\/form&#8221;<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"626\" height=\"241\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/form.png\" alt=\"Form\" class=\"wp-image-8641\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/form.png 626w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/form-300x115.png 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><figcaption>Form<\/figcaption><\/figure><\/div>\n\n\n\n<p>Enter <strong>1234<\/strong> and hit submit<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"626\" height=\"241\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/login.png\" alt=\"Login\" class=\"wp-image-8642\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/login.png 626w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/login-300x115.png 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><figcaption>Login<\/figcaption><\/figure><\/div>\n\n\n\n<p>And there it is, Our Flash message right on the screen. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>That\u2019s it for this tutorial, guys!! I hope you gained enough knowledge to use the Flash method in your web applications. Do check out our <a href=\"https:\/\/www.askpython.com\/python-modules\/flask\/flask-forms\" class=\"rank-math-link\">Flask Forms<\/a> article to know more about Forms<\/p>\n\n\n\n<p>See you guys in the next article !! Happy Coding !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn how to flash messages using the Flask flash() method. What does it mean to flash a message? It is always good for GUI applications to give feedback to the user for his actions. That is, for example, in Flask form, if the user leaves a field empty, it is [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":8644,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-8619","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flask"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=8619"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8619\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/8644"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=8619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=8619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=8619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}