Hello guys, welcome to LearnVern.
In the last topic, we saw Register User with Server-side validation in which we did registration of user & along with registration we performed server-side validation.
Right?
Now our today’s topic is Login user with server-side validation.
In the last video, we registered a User, right?
But today, we will Login this user.
Login means we will give it access to that website.
Right?
So let’s move forward today.
First of all, what does User login mean?
User Login is used to manage.
Manage what? To manage user profiles.
To know how many users are available on your website, and the certain users who are logged in, they can update, edit & delete their profile.
Right? Clear?
Through Login, we can also know how many users are logged in at time on your website.
Because of this, the owner gets to know that their website is getting used.
That the users are using it.
Alright?
That is why Login is the most important part for Website Development.
Right?
So now if you are logging in, Session Management is used here.
Okay, so what is Session Management?
Session Management is a Mechanism with the help of which, you can store your information server side.
Okay?
With the help of which, your data is highly secured server side.
Where?
It is secured server side.
Okay?
And all of this data interacts with the database.
Okay?
Which means nobody can steal your data from that website.
Okay?
Session works not only with databases but also file base.
File base means the data which you stored in file & then you upload that particular file on the server, right?
So it works along with that as well.
Okay?
In short, it is the most secure part when you login.
Because with the help of Login only, your session will start working on your website.
And it expires only because of Log out.
Right?
What happens?
Your session expires.
Right? Clear?
I’ll tell you how does the user Login, how to work along with the user, and all other things practically, okay?
Moving forward…
When we talk about session management, Django has a Middleware.
So what Middleware does is, it manages Session.
Session Management means this Middleware handles Sessions in Django, which is installed, which is already installed in your settings.py.
Right? Clear?
And when you create your session, means when you login, a method is used which is request.session, right?
Which sets your session.
Okay?
With the help of which your session is set in your website.
Right? Clear?
Understood?
When I show you practically about how the session actually works, that time you will understand clearly how the session works in reality.
Right? Clear?
Let’s move onto practicals.
So we will open CMD first.
Right? Clear?
We will continue in the last project only where we will continue with our last template only.
Right?
We will activate our Virtual Environment first of all.
And we will open our VS Code too.
And what else is remaining?
Our Database.
I’ll open my database as well.
Right?
So I opened Admin here.
Our database has been opened here too.
This is our database in which we have registered 1 user which I showed you in the last video.
Now with the help of this user, we will perform the login of this user.
Right?
So we already have a View made.
Now what is the first thing that we have to do?
First & foremost, we have to make a login template.
So we already have a login.html available.
What will we do ?
We will copy this registration template & paste it on login.html.
What will we do here?
To login, we only need that particular user’s email & password.
What we will do is, we will keep only the email & password field here.
And we will erase all the other fields.
Okay?
I don’t want Contact, I don’t want Confirm Password as well.
Right?
And here, instead of Sign Up, what will we write?
Sign in.
And here too, we will keep the form name to Sign In form.
Right?
You can see ‘Don’t have an Account?’ here.
What will we write?
Register Now.
Okay?
Now we will run our Login page & see.
We’ll have to make a View for Login page, which is Def LoginPage.
In which we will pass our request.
And here, return render to request app/login.html.
Okay?
So students, we have made a new function, that is a new view.
Which view did we make?
Login View.
Which view? Login View.
So what do we have to do next?
We have to make its URL.
I had told you in the last video as well, in the Registration video.
Those of you who haven’t watched it, follow that video once & then continue with this video.
Because this video is dependent on the previous video.
Okay?
So I’ll make a login page URL here.
Then I’ll write views.LoginPage, which is our View name.
And pass the Login Page here.
Okay? Right?
Now we will turn on our server once.
Which is python manage.py runserver.
We have turned on our server.
We will go here once.
Now, I want to open the Login Page directly.
Right?
What will you do?
You’ll apply / & what will you do?
This name you have given in this particular URL, right?
We will copy that name.
Then go here & paste it here beside the browser name.
As soon as you press Enter, your Sign in form is ready.
We had updated a little bit here, where we had passed Email & password.
And we had changed the Sign in button name here.
So this is our Sign in form.
But right now, we only have a form here.
We haven’t had the user login yet.
How will we get the user login?
Let’s see practically.
We’ll go towards our View.
Views.py.
And here, we will make a new view which is Login User.
Okay?
And in this view, we will Login our User.
I’ll name the View as a Login User.
Okay?
Now, the first thing is, when you are login in your user, through which method is your form passing your data?
Let’s check.
So through which method is our form passing the data?
Post method.
So firstly, we will check if request.post, sorry request.method == post
If request.method == post.
See, we have used it above as well, right?
That is, is our form value coming from the Post method or not?
If it is coming, then only it will go inside or else it won’t.
Okay.
So here if the request comes from Post method, what do we have to check?
First of all, we have to get two things in View.
Which two things?
First of all, we need the user’s email.
Request.post.
We will get its email here.
And what do we need of that user?
We need its password.
Request.post password.
Okay?
Right?
Now what do we have to do?
We have both of these things now.
Why? Because we want only these 2 things.
Because, on our login form, we have kept only and only email & password.
We will perform our User Login only with these 2.
Right?
Now what do we have to check?
The email ID that is arriving.
So, checking the Email ID with the database.
Okay?
So if the Email ID which the user has input is in our database, then only the user can login.
Which means if the Email ID is verified with the database, only then the user can login otherwise that person cannot login.
Okay?
How will we check it?
So I took a user named variable.
What was our model name?
It was a user so we’ll take model name user.objects.get.
Why get?
Because if we get an Email ID in our database, then we have to get all the data of that particular user in session.
Where? In the session.
Okay?
So how will we do it?
The first email that will be passed, from where will it come?
From models.py.
And the other email that will be passed, from where will it come?
From Views.
Okay?
Now first of all, we have to check if the user is there, if the user is there, right, then the password he had entered, that too we have to match it with our database.
If we don’t match that password with our database, then we have to pass a message to the user in which it will send a validation error that the password you are entering is not matching.
Okay?
How will we check that?
Let’s see.
If user.password.
Now why did we write user.password?
Because the email ID through which we will get the user, will be stored here.
And we have to check the password of that particular user as well.
That is why I wrote user.password which is == password.
Now which is this password?
It is the one which it entered in View.
If it is matching, if the password is ==, then we have to bring all of its data in the session.
Okay?
We have to bring all of its data in the session & to bring it in the session, you will use request.session method.
Which method?
Request.session method.
Request.session method which will create your session.
And in that, suppose I want its first name.
How will I get it?
User.First name .
Now this user dot first name comes from your model.
Where did it come from? From the models field.
Means The name from the table’s field.
See you have taken this table’s field here.
You have to pass the same field name in this user dot first name.
In the same way we want request.session last name too.
So user.last name.
In the same way request.session.
What do we want?
I want its email ID as well
So user.email ID.
So I want these three things of the user after login.
And as soon as we log in, what do we do?
We do return render.
Okay?
On which page? On the homepage.
So here I will pass request.app/home.HTML.
Ok right?
Now the three things that you passed in the session Ok?
I'll write here for you, we are getting our data sorry user data in session.
Okay?
So the things that you have passed under session, now with the help of these meaning the things that we passed under session, will be available on any page on our website.
You can get the data which is in this session on any page or print them as well.
Let's see how.
So first of all, we need to know validation as well.
For that, the else part will also be made .
So else,
And we will write down a message here, That password does not match.
And we will return render.
On which page? Our login and HTML only.
Ok right?
So what will happen is that if your password does not match with the database, What it will do is it will bring you back to the login page and print your message as well.
How do we have to pass the message? In the dictionary.
Right?
I had told you the same in the last video of registration as to why do we pass the message?
We pass the validate message to give the validation.
And here too there will be an else.
What is this Else for?
The user email ID you have entered, that user does not exist in our database.
So what will we pass in the message?
User does not exist.
OK? Then we will return render it.
Request. On which page will we render it?
On the registration page.
Those of you who assumed this, have assumed it perfectly.
So on register . html, What will we do?
We will again pass the message that we passed above, okay?
Right? Clear? Okay.
So our new view is made, log in view.
Now what do we have to make? We have to make a URL.
So we will go here and make a new path for login User.
Ok?
And here, We will write views.login User name = login.
Ok?
And Where will you keep login after copying it?
We will pass it in the form inside Action in login.HTML.
Okay?
Here we have talked about the home page as well.
So we will have to make the homepage too.
So we will write here home.HTML
Right?
Now here in the H1 tag, We will simply write home page So that we can know that the user has logged in.
Okay? Right?
Now what we have to do is, the user that is logged in, we want to show his first name & email ID.
Let's see how to do it.
So I’ll write first name here.
Okay? Now we want to display the data.
We have to pass the value.
In the CRUD operation video when we showed on the browser, we had written I.ID.
If any of you haven't seen it yet, I urge you to watch the CRUD operation video first.
How will we access the data here?
Request.Session.first name.
Why in this way? Why did we write request.session.first name?
Because, your data is coming here on this page because of a session.
That is why it is compulsory to write request.session, otherwise you won't be able to access any data from here.
Okay? Right?
Then I will use BR for the break line.
I will write the last name too of the user who is logging in.
You can print the last name like this as well.
Request.session.last name.
One more BR for email ID.
So request.session.email.
And we will display this in the center, this whole thing, so that it will be seen in the center.
So we will take the Center tag here.
And I will end the Center at last.
Okay?
Whichever user has logged in, we will be able to see his First name, last name and email displayed on the homepage.
Let's see, let's run it.
I will Refresh this page.
First of all, we will login this user that we had last registered.
So what is our Email ID? s@gmail.com
And password Sunit 123.
So s@gmail.com & password Sunit 123.
Press enter and you can see that the first name, last name and Email ID of the user has appeared here.
You can check it from here.
See, the first name, last name and email ID.
Right? Clear?
Understood How the user is logging in?
Right? Clear? Perfectly?
Now let’s go.
Now let's see about a user which is not in the database, let’s see what happens when he logs in.
Let's see.
Suppose I write r@gmail.com & I wrote the password r123.
We can see here it is written user query does not exist.
And it will bring us to this page, why?
Because the user query does not exist.
Which means it did not get the user itself.
So it will bring us to this error page.
Right?
so if you enter a valid user, that valid user will login and his details will be shown on the homepage in this way.
So in this way with the help of a session you can do the user login on your website.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
In the next topic, we will see Uploading images through Django, in which I'll show you by uploading the images and then show them on the browser.
That with the help of Django, how we can upload the images and then show them on the browser.
Share a personalized message with your friends.