OK, let's talk about jingle messages, so message is basically a way to send a little bit of information
from one page to another, but it only does it once.
So if you're familiar with the idea of sessions, basically it creates a session and once that session
is access to delete it.
And so we want to add a little message, a little session.
And when someone fills out this form and it takes them back to this page, it should say, hey, you
successfully added an image.
And this is really, really easy to do, and this comes Bakhtin with Django three automatically.
So let's open up our editor here and let's open up feed viewers that pie.
And first things first.
Let's go ahead and import.
We want to do from Django Cantrip, I believe, on Trib.
Important messages now, if you're learning Django and you're working with an existing Django project,
you have to go into your settings and make sure that messages is in there.
So if we open up our settings and type in messages, Django Cantrip messages is in our installed apps,
it's in our middleware and it is also in our template context processors, which means it can be added
to every single template.
Doesn't matter what your template is, it can just be automatically, automatically added.
And so you need to make sure you have those in their.
So for now, we're going to do from Jagoda Cantrip import messages, scroll on down to our form, and
when that new object is created, let's add a new message and so we can add a new message with message
dot add message.
It takes a request by default.
Let's do messages, Dot.
Success, there's different types.
We'll take a look in just a moment at the docks, your.
Post was successful.
And the tape in Django Messages Dock's.
The messages framework.
And so it tells us that we need to have all this stuff set up in our middleware installed apps, contact
context processors, et cetera, et cetera, storage backgrounds, message levels.
OK, so this is where I got messages, dot success and instant messages.
It's messages.
That's what I imported.
Messages, ad message.
Messages dot success.
And this is where I got success from, so if it was successful, I'm going to give a success message
and this is going to be accessible in our template, if there was an information, we could use messages,
info, debug warning or error.
So we have these five options.
I'm going to go with success because I know that that is going to work well with bootstrap, because
Bootstrap usually has like I think it's big Dasch success or text Dasch success or something like that.
So I know that I think actually all of these might work except for debug, but I want this exact color
one down the road.
And yeah, if we go down into message tags, it actually tells us so success is going to give us the
tag success and that's going to be put into our TML success attribute or class attribute.
And then how do we do this, it says, adding a message so we import our message, we did that already,
messages and message, it takes request as its first parameter.
A message type is that info success, debug error, et cetera, and then the message itself.
And so here are a few different examples.
And when we want to display messages, we can simply copy and paste this.
Let's do this for the time being.
This is going to be fine.
We can clean this up a little bit later.
So let's save this.
And let's go into.
Templates based on to close a database one, and because this is accessible in every single template,
because it's in a context processor, which means it's accessible on every template, we can now say.
If there are messages, create a list and unordered list and then loop through every message.
With an ally and says if there's message tags, add the tags in here, that's going to be the success
or debug.
Or what were the other ones in your success, warning, error and debug?
And if and then the message itself, so it's going to loop through all of them, we can have multiple
messages that's important to know and that's why we're looping through these.
Now, let's go ahead and give this a try, and I'm going to show you that this actually has some form
of an error.
This is going to not work.
You know, just like any image.
And it says, oh, name, air, name, request is not defined.
Now, here's the tricky thing, and you're going to want to take note of this, because moving forward,
this is going to be a life saver, so.
The first parameter in ad message is request.
If we go back to our code here, inform valid, we don't have request, we have self and we have form.
So how do we get requests?
We're in a form of you here.
Let's take a look at where request exists.
Get when you get the form, so when you're displaying the form, you could access the request, their
dispatch http method not allowed options post and setup.
And so what we want here is we actually want dispatch.
So when this view is being dispatched, when it's being when it's about to be rendered, what is it
going to do?
And so let's go ahead and just copy this line.
And because it has request in here, but our method in here form valid does not have request, what
we can do is.
Def Dispatch self request args Quark's.
I literally just copy that from here.
Def Dispatch Self Request args Quark's.
Return super.
Dispatch request ARGs and Quark's.
And so literally, at this point in time, we're saying, hey, Jango, we know you have a dispatch
method, I just want to let you know that I want you to use that dispatch method.
Honestly, this is pointless, but it becomes less pointless when we do this self-taught request is
equal to that request object.
And because we're in a class, we can use self-doubt request anywhere else.
So now we can type self-support request here because it's being access.
Your dispatch gets run before form valid does.
So it's going to set this for us and then we can access self-taught request down the road.
Let's go ahead and try this out.
Let's just refresh this page, generate a new message.
And I'm going to grab any image doesn't really matter.
And it says your post was successful, and that's coming from are based on e-mail.
It's in an unordered list, and that's that's right, click and inspect this, we have you all with
a class of messages.
We have an ally with a class of success.
And in it has your post was successful.
We have a ule.
Class of messages, it's going to loop through every message, we have got an ally with message text
message tags, that was success that comes from here.
And then the message itself is your post was successful and says the message in there and that's all
there is to adding messages.
So really all we need to do is type messages and message.
It takes a request.
It has to take that exact request object.
It takes some sort of message type or a tag, so we're saying messages does success because this was
successful and then our message itself in our base template because we want to show up on any page.
It doesn't matter where we wanted to show up on any page.
We're going to say if there are messages, cool, cool, cool, cool, cool, loop through those messages
and display them.
Now, watch this, when I go back here and it refreshes page, that message goes away.
It's very temporary.
It only exists once.