Hello guys.
So we are going to continue the discussion with respect to flask.
Uh, already in our previous video, we have seen that how we can probably integrate a HTML file with
our flask app itself.
And we had also seen the functionality of render template.
And one very important thing, I hope, which everybody knows is that we have to probably create our
HTML page inside templates folder.
Okay.
And that is why you will be able to see that whenever we use rendered underscore template, we will
be able to redirect it very much easily.
Now in this video I'm going to talk about a very important topic which is called as HTTP verb, where
we are going to discuss about, get and post.
So overall in HTTP verb there are four types get, post, put and delete, put and delete.
We will discuss in the upcoming videos, but right now in this particular video, as a starter you really
need to understand about get versus post.
Okay, now uh, let me do one thing.
Let me go ahead and create one of the file.
Okay.
So I will just go ahead and write get post.
So get post.py file is basically there.
I've already created this.
I will copy this same thing and I'll paste it over here just to show you the new execution with respect
to get and post.
Okay, now here you can see let's let's consider this particular route okay.
Now in this route when I'm using slash index right.
First parameter inside this route is rule which is nothing but str.
And second parameter can see options.
Inside this there will be one very important parameter which is called as methods.
And by default the methods.
Uh, value is nothing, but it is simply get okay.
So get you will be able to see the method value.
Okay.
Now to make you understand the difference between get and post, let me just go ahead and open my browser
over here okay.
So let's this is my browser okay.
Now let's say that I am going to search for google.com okay.
So once I execute this, you'll be able to see what has actually happened in the back end, right?
We are able to see some kind of content over here.
Over here we hit this particular URL.
This URL is going to get mapped with the uh.
You can just consider that it is going to get mapped with the web server.
And from that particular web server, it is going to interact with the Google Web application.
And then the default content that needs to come, it will get displayed in front of us.
And that is what is basically getting displayed over here.
So this actually becomes a Get request.
Okay I'm just hitting a URL.
I'm getting the content.
Okay.
I'm not posting anything right now from this particular, uh, front end basically.
Right.
I've just hit the URL.
I'm getting some request.
Okay.
Now to make you understand the difference between get and post here, let me go ahead and write Krishna.
Okay.
So once I go ahead and write Krishna, you'll be able to see that I'm passing this entire information.
Okay, as a query to the Google search engine.
And now that based on that particular query, I'm able to get some response.
So this exactly is the example of Post request.
So here you can see search query is equal to Krishna.
So as soon as I'm hitting this right some some input is going to the web server over there.
And based on that it will go ahead and interact with the web application, which in turn will communicate
with the database and probably get the information, crawl the entire information and get the information
about Krishna right.
This is how simply you can probably consider as an example of post.
Okay, now what we are going to do is that I will go back over here and let me just show you that specific
example.
So here what I'm going to do, I'll just go ahead and write methods is equal to by default.
If I don't give anything it is nothing.
But it is a Get request.
It needs to be in capital letter.
Now I will go ahead and quickly execute this and let me go ahead and write Python app dot Pi.
Okay.
So I will just go ahead and execute this.
So here you can see Python app dot pi, not app dot pi.
It is get post dot pi because this is my file name.
Get post dot pi.
Okay, so if I go ahead and execute it so it is giving me an error.
Python okay sorry, it should be get post okay not app dot get post.
So here you can see I'm getting the URL over here.
I will copy this okay.
And I will open my browser again okay I will go ahead and hit this particular URL okay.
So perfect.
You'll be able to see that.
Yeah I'm able to get it over here right now.
Uh what did I hit okay.
So here you can see this is my home page okay.
Now I will go ahead and hit my index page and index page.
You know it is a get.
Now as soon as I hit the index page this is the information that I'm actually able to get.
And that is what we'll be able to see that because of this particular Get request.
Okay.
Now this is fine.
But I also need to show you an example with respect to the post also, right?
So let's now work on the post request.
Now for this post request, what I am actually going to do, I will go ahead and create my route.
Let's say I will go ahead and create a form dot HTML page.
And here I'm going to basically write form dot route.
And let's say this will be my slash form.
Inside this form I'll put some of my personal information.
And I should be able to capture that post request as soon as I click submit button.
Okay.
Now here you will be able to see that the methods that we will specifically be using will try to use
both get and post.
So first of all, let's go ahead and use get.
And the second one we will go ahead and use post okay.
Now how to probably get this particular Get request right.
I'm considering that if I go ahead and hit this slash form URL, it should give me some kind of HTML
page.
Okay.
And that HTML page can be a form dot HTML page.
So I will just go ahead and write definition form.
And here we will go ahead and write if request dot method is double equal to post.
Okay.
Now the request method that we are specifically using this will be responsible in capturing the post
or get request.
Okay, just by using this function from the browser it will be able to capture.
Okay.
So what I will do and this request library will be available inside the flask itself.
So let me just go ahead and import this.
Okay.
So if request dot method is equal to post I will just say pass for now because uh, over here you will
be able to see that if this is a Post request, I should be able to capture the information inside this
that I will show you in the later stage.
Okay.
Right now I will just say, hey, if it is not a post, right, just go ahead and return render template
and just redirect it to a new HTML file which is called as form dot HTML.
Okay, now this form dot HTML will basically have the form where I need to insert some information.
So now if I go back to my form dot HTML.
So this is the form dot HTML that I have created, I will just go ahead and create the simple HTML.
Okay.
So here you can see title head body and submit a form.
Now this is very much important okay.
And here you will be able to understand what is the main thing over here.
So here what I have done I have used this particular form tag.
The method is post.
I have kept a label for name and one text box where I can put the information about my name, and this
is a submit button.
Now as soon as I click the submit, since we know that it is a post method, it is going to hit which
URL it is going to hit this slash form URL.
And that is the reason here you will be able to see that I have this particular form right.
So it is just going to hit slash form over there right now when see if I am first of all time I'm loading
form dot HTML.
If I go ahead and with respect to the URL and write just slash form dot HTML, I will be getting this
entire page, but after I fill this information, post it.
Now I need to handle it.
That entire information over here.
Okay, so let me just go ahead and execute.
Right now I've just written it as pass.
So I will just go ahead and write over here.
Let's go ahead and write Python get post.
And and here we'll be able to see that I will just go ahead and write slash form okay.
Just execute it okay.
I can probably see this becomes a Get request right.
And I can just go ahead and write my name okay.
Like this I will go ahead and write my name and I will click on submit.
Now see if I click on submit.
Nothing is going to happen because I was not able to capture anything.
Right?
Because this submit button, I need to probably make sure that whatever condition I have written over
here, I need to do something over here.
Right?
So now let's go ahead and handle this.
And here you will be able to see that, uh, I will just go ahead and handle this.
So I'll say, hey, if the request dot method is post, Then we will retrieve the name value and that
we can retrieve.
By using this request dot method.
And here I'm going to give my id as name.
Why?
Because in form dot HTML.
If you see the type of text here, the id name id is equal to name, right?
We need to retrieve this entire information so that we get the text information that I'm actually putting.
Right.
So once I get it over here, what I will do, I will just go ahead and write return F.
Let's say I'll go ahead and write hello, hello.
This specific name.
Right.
Whoever is the name person.
And here I will just go ahead and execute it.
Now this is the way just with the help of one root function, I am able to handle both get and post
for post.
I can also make sure that I can redirect it to another HTML page.
And then we will see that how we can also call this name to that particular HTML page.
That is where Jinja2 template engine technique will work, right?
So right now let's go ahead and execute this much and see whether it is working fine or not.
Okay, so first of all, I'll just go ahead and open my command prompt again.
This got deactivated.
So this is my venv environment.
I'll go ahead and write.
Hey CD 13 flask CD flask.
Please go to that particular folder before executing it.
And then I will just go ahead and write Python get post.py.
Okay, so once I execute it here, you'll be able to see that I'm getting this particular URL.
Now let's go ahead and execute this particular form.
Okay.
String indices must be integer.
Why I'm getting this answer.
Let's see.
So let's go ahead and write this crush like let's submit it.
So here you can see that I'm getting some error.
Okay.
And here it says hey request dot method uh of name okay.
So I made one mistake.
It should be request dot form not method okay.
Request dot form okay.
Request of form dot name.
So now let's go ahead and execute this.
Reload reloaded.
Okay, so now let's see this.
Okay.
Now here, I'll just go ahead and write Christina.
Now, if I go ahead and click on submit.
Now what has happened?
This post request has got successful.
It has probably retrieved the name and it is just displaying.
Hello, Chris and hello Christina.
So I hope you are able to understand this particular video.
Now in the next video what we are going to do now, it is quite amazing because in the next video will
be quite exciting because now we are going to use the Jinja2 template engine where I will show you from
here how I will be redirecting to a template to a HTML file.
Okay.
And there I will be filling some information with some data source.
And the data source is nothing, but in this case this name variable, right?
With the help of this name variable, I will be putting some information in that particular HTML page.
Right.
How you can actually do it.
And that is what we are going to learn uh, about how we can use templates using Jinja2.
Right.
And Jinja2 template engine.
So I hope you like this particular video.
This was it from my side.
I'll see you in the next video.
Thank you.