Hello, friends, in this video, I'll tell you about the message flashing mechanism in a flask application.
Mean you are basing application.
Uses a kind of a mechanism by which the user is prompted or told about certain activity or events that
are taking place are happening.
So this mechanism of providing the feedback to the user about it's about his interaction, such a mechanism
or such a feature available in the mini desktop applications built in languages like Java or dot net
languages, for example, that Java has a feature of dialogue.
There is there are dialogues boxes that can be created.
Languages like visual, basic or C Sharp has a facility or a class message box of both is the same.
It flashes the message on the top of the underlying frame.
For example, the name of the file is already present.
Do you want to write it?
This is a dialogue or the message that is popped up when a particular kind of activity takes place.
So such a dialogue or message box is generated by their desktop applications.
Similar kind of activity is also finally found in JavaScript.
JavaScript has a function called as alert, so it pops up a message to let the user know that what is
to be done or what has happened.
So the same purpose.
The purpose that is provided by their dialogue, message box or alert in flask, it is provided by the
message flashin.
The flashing system of this flask, it makes it possible to create a message, it will create a message.
We have seen that when a particular order is given, the associated review function is executed during
the course of this execution.
If a certain message is to be displayed to the user, then the inside of you function flash.
Flash is used.
So what is what it will do?
It will create a certain message and that message will be passed on to the next of your function.
Remember that the flash or the flashing will not be done in the same you function in the flashing has
been generated.
So the important thing to note is that it makes it creates a message in one view and render that message
in another view or the next to you.
So the flash method passes a message to the next request that is very important.
Right.
So the syntax of function is that it takes two arguments, plus there is a message, the message that
is actually to be flashed on the client's browser.
And the second is the optional parameter or less category message parameter is the actual message category
parameters optional.
Three types of categories can be mentioned error info or warnings.
So what kind of message it is accordingly, on the next you function or on the next to you are, the
user will come to know that this is an error or this is the information, or is it a warning to such
a flash function can be executed in one view and on the other view the flash will be displayed.
Not now that one view passes the flashes to another view that it is the duty of the next of you to display
those flashes.
So what to do the next time you remove those messages from the station?
Those flashes will be stored in the station from that station.
Those will be extracted, removed, and they will be used to display with the help of a function.
But let's get flashing the messages.
The syntax or prototype of use of this get flashed flash messages is like this get flashing messages.
Both arguments are optional with categories and category filter.
If they are flashed, the messages have certain categories, then there will be a value to this parameter.
And if there are any categories, if there are multiple categories and you want the viewer to display
only those flashes or those messages of a particular category, only you can apply the filter.
So get flashily messages to Offaly barometer's.
Maybe you're not a may not be OK.
Now this get flashed.
The messages will collect all those messages and how these messages messages are processed.
They are processed during or inside the template that is being rendered by the first of you function.
The first of you function will call another.
You are in by a render template and that template will have some such mechanism.
What will happen first statement is first, a template statement is with messages equal to get flashily
messages, there may be any optional parameters like this or not.
So what will happen this get flashed?
The messages function will find out whether there are any messages to be flashed or not.
If there are any messages, there may be multiple messages.
So there will be a for loop.
So one message at a time will be flashed on the browser.
Of that line.
So this is the eve and this is done with such a nested arrangement of wit and with if and if any, for
and for those will be used on the template code.
So with the help of this, the messages that are received from the previous view will be will be flashed
by the next room.
So such a mechanism now will have a very comprehensive example which will generate the messages and
those messages will be passed to a template and that template be displayed.
The messages set an example I will show you with the help of those examples, we will come to know or
understand better the use of messages and flashing in a flask application.
So this is a demonstration of the message flashing in the flask application.
And for that purpose, you will have to import the flash function in our last module.
So in addition to the other required functions like redirect the render template you are for, you'll
also need a request object and of course the object to be important.
So those requirements are important in the beginning.
Then you of course create the application object set of the.
Secretary.
First, you are defined is backslash.
And bound new function index, what it is going to do, it is going to render their template or index
not estimate.
So what is their index not estimate?
This is the index, not SGML.
Now that.
Index view function is likely to be executed when.
It is to the user is already logged in and it is he's trying to log out.
Or the logging is unsuccessful.
In either cases, your flask application is going to generate some.
Flash messages, so before.
Displaying this part of the index, not if there are any messages to be flashed, those will be done
by this school.
But of course, now now that you are just starting the application, there are no messages to be flashed,
so therefore this condition will be not true.
So therefore, the this entire segment of the court will not be executed.
You'll only see.
Do you want to log in and do this logging work?
That is a hyperlink leading to your all for logging.
The other.
You are a little different in your Python application.
So we'll come back to this afterwards.
What what is the purpose of this right now?
There will be a simple hyperlinked, do you want to login and the hyperlink will lead you to you are
all for logging the logging view function that is defined in your application.
So we'll have a look at that.
So this is the slash log-in you are in, which is defined by the route, the greater taking to arguments,
the you are in and the methods.
Again, when this log-in you is visited for the first time, obviously the request not is equal to this
condition will not be true, isn't it?
This is not true.
So therefore.
This part will not be executed.
So therefore, you will.
Come across this.
Right.
So.
It will render the template cold logic, not estimate.
Now you see that the render template function is making taking two arguments.
One is the.
And you know, the template to be rendered login that e-mail and there is a.
Error parameter, the value of the error parameter is the default value of the error parameters defined
as none, because initially it will be not.
Then what will happen this log in the e-mail template will be rendered with error is equal to none.
OK, so what is the legendary.
This is the estimable logging estimate in which there is a.
Ginge to templated court, if at all, there was any error that was raised before coming to this, that
error will be displayed.
Otherwise it will display form where there are two text text boxes.
So to save one for username adelfa the password, and then there is a submit button which will post
the data to the same.
You are in log.
Right.
So that means if you click the submit button, the data will be posted the same slash login.
You are the view function now.
For the second time when that log in.
You are visited, the Log-in view function is executed, another method is post.
Isn't it so?
We will have a look at that log in are religion and see what happens.
So it comes again here.
Now the method is post.
So this condition is true.
No, it will verify whether the username and password are admin admin.
If they are not admin admin, then it will raise an error, the variable error will be initialized to
invalid username and password.
If, on the other hand, if the username and password the values of the two data fields in the in our
e-mail form.
Do have the value of admin admin, then the message will be, you are you will successfully logged in.
And there will be another flash log logout before the logging, before logging again.
OK, and taking these two flash messages, it will go back to the index.
It will go back to the index you function.
Right, so we'll have to revisit our indexer testimony and then analyze.
The, you know, segment of the ginger to template wherein the get flashed messages are processed.
So this is that part now that the index, not e-mail, is going to be rendered for the next for the
second time.
There are.
The messages to be flashed, those will be collected with this and for each if there are any messages.
If there are any messages, the messages will there will be messages object, so if messages, yes,
so far, each message in these messages, that message will be displayed.
Right, so this will display all the messages in our case, there are two that are going to be two messages
if the logging is successful, that there are two flash to cause the flash flash function.
So there were two messages.
Those two messages are going to be displayed by these four.
OK, so this block will display all the flash messages generated by the earlier view function.
And then again, of course, after the messages are flashed, you again have this hyperlink to re login.
OK, so this is the slightly complex explanation of the flushing mechanism in our flask application
will do these activities one by one.
So our Python code for this entire flash activity is written in flash, not ever, so I will start the
server and I will go to the browser to visit the localhost 5000, slash whatever.
So I will enter that you are into the browser's address bar.
It is giving me a hyperlink to.
Take me to this login page.
This is the login page.
Now I will show you both the scenarios where.
I use some random.
Username and password, now what will happen, the error will be raised through the error message will
be displayed.
Second time I will give you a username and password that is acceptable.
And then log in now, the messages will be flashed.
You were successfully logged in longer before the log in again.
OK, these are generated by.
The flashes that were raised by the login function.
Do you want to log in, it will again take me back to the login page.
OK, so this is the entire cycle of events that will that the application will go through for the message
flashing.