Hello guys, welcome to learn vern.
In the Last topic we saw registration template integration in which we learnt about CSS and how to integrate registered template after using or downloading at the same place right?
Now what we will do is register the user with the same registered template.
And in the same registration, validate the user with server side as well.
Now first thing, what is user registration for ?
User registration is used to register your user on your website by using registration form.
So that particular owner can keep the record to know the available user using his website.
And through this particular process, you can validate the user as well, that the user which has registered is a validate user or an existing user in our website.
So this is a very important process for websites.
So I will take this thing practically, How can we perform this practically okay? Right?
We made this project last time where we integrated a registration template, we will run the same project right? Clear?
Here first of all we will activate our virtual environment.
So, we will do script activate and then python manage.py runserver
Let's run a server to check which particular template we have integrated here.
So we integrated a template something like this right?
Now in this template, I want to do some modification. right?
So you can freely do modifications as well, there are no restrictions okay?
What you will do is in the html page, register.html page that we created last time, in that suppose that i have to take input for the contact right? So here i will write at the place of place holder ‘contract’.
Here I will take name as a contact right?
I take text as a type, it will work and here we will take on more input for the first name and in this username I will take the last name.
Here I will take L name and here name of this input as F name right?
And set the input type as text right? clear?
Now i will refresh here,i have this many fields available here in the form okay?
Now this ‘ i agree to terms and conditions’.
We are not keeping any such condition so we will rub this ‘div’.
So we have that much space now and the registration form is ready okay?
Now here we will make some changes, let's make this sign up form and here also sign up form.
We will get the view perfectly okay?
So this is our sign up that is our registration form ready okay?
Now I have to make the user register through this particular form and validate as well.
So how will we do it? Let's see.
So first of all i will create a view here that will register the user but before that we will create models okay?
So models will be created according to template.
So what are the fields that we have in the template?
First name, last name, email, contact, password and confirm password.
Right? Clear? Ok!
Firstly we will create our model.
Here we have taken the class as a user right?
And here models.model okay?
Now what are the fields that we want, if we have a look at our fields.
We have First name, last name, email, contact, password and confirm password.
Now here how many fields we are going to create in total?
First we will make a first name which is models.character field and in which we will take max length as 50.
In this way, we will do some copy pasting for the last name, email.
For email we will take here email field then for contact, let's leave character field for this.
Last field we will take a password.
We won't take a field for confirm password because confirm password is only used for validating.
That is not used to store data in the database okay? Right?
So here we made a particular model.
Now first of all what we will do, we are integrated with MySql so with Mysql we need to install pip.
Pip Install - - only mysql client
So we had to use this command.
Okay so we installed this.
Now we have made our custom user model okay?
So we will migrate it, we will make migrations.
So make migrations, so our one model is created and then we will migrate it.
But before migrating.
Hope you are able to understand me.
What you have to do is create a database here.
So first we will create Here is the database for which I have kept the name as registered user clear?
If someone doesn't know how to integrate mysql then i have taught you mysql integration in crud application.
Do watch that video first, only then continue with this video okay? Right?
So here is the user registered, then we will migrate.
So here as we will migrate, it is migrating, so done migration.
As we will use this, here is our table made which is first name, last name, email, contact and password.
So these were models made right?
Now we will create our view.
For what purpose will we create a view?
To register the user right?
So here we will create a new view.
View for user registration okay?
So here i have created a view, user register right?
In which, what did I pass? A request.
Now see guys, here what we have to check first is the method of the form that it should be ‘post’.
First of all we need to check that right?
So the method of our form is post.
So we have taken the form so we will not forget to add here csrf right?
You already know why to add csrf and if someone doesn't know, watch it in a previous video of crud application that explains why to use csrf token.
>
So first of all, how will you check?
Here you will check through ‘if’ condition.
If request.method == post, is this post or not?
If it is post, only then it will go inside right? Clear?
Only if the method of the form is post, then only it will get the data. okay?
So here if it is post, then we will get all the data.
like fname which is request.post, here we will take fname.
Then Lname, request. post , we will take Lname.
Similarly email, here we will change the name of the variable which is email.
Then we need contact, so here contact and here also we will take contact.
And here we need our password, so here we will take the name of the variable also password okay?
Now first thing, we got all the data right?
We need to check, what do we need to check ?
So first we will validate that the user already exists.
So first we will check that the user coming to your website does not exist already.
Right? Clear? He is not already registered in your database or website.
So how will we check that?
Through the database, through tables.
First of all What we do here is import our model, right?
So we imported the model.
Here I will take a variable named user, and what is the name of our table, user.objects.filter okay?
And here we will track it through email, which means to check that your particular user registering with the help of email id should not be already registered right?
So where did this first email come from? Name of table field.
And where did this second email come from? From view okay? right?
So here we validated the user, now how will we validate
Now the particular email id that user will enter will get matched.
If it matches, then the first condition, if the user..
If it gets found, the email id that user entered gets in database then we will pass a message that
‘User already exists’ and then we will return it, where?
Return render request app slash register.html.
And how will we pass this message on html ?
Simply you will use a pair of keys and values.
So I made a key of ‘msg’ and passed the message with it.
This is if condition part, if you find the user then we will pass a message which is ‘user already exists’ right?
Now the else part, what is in the else part? if you didn't find the user.
If we are not able to find the user, what we will do is, the user which is not found, we will check for his password and confirm the password. Ok?
to check the confirm password first of all we need to take the field of confirm password here which is request dot post.
I took the name 'C password' of a field.
we will check here 'c password' which is confirm password key, and has to be taken as the name of the input. right?
So here we will get the C password that is the confirmed password.
Now we need to check the confirmed password and the password he is entering.
So how will we do it? all these things that we are checking are server side validation ok? right?
so we will check if password equal to equal to C password.
right? if it is meaning the password which he has entered is equal to equal to confirm password then only user will get registered ok?
So how will he do so? See here I took a variable as a ‘new user’ right ?
user dot objects, now we have to insert that user which means to register that user, so to insert which method do we use? create method and with its help we will insert the user here, by passing all the fields with their attributes ok?
first name then last name of the user ok? then we need the email id of the user ok? also we need to store the contact information of the user.
I'll take this below so that you can see it perfectly, so contact also, what do we need else? password.
so we need to store the password as well which is equal to password ok?
so here we get all the fields, we will not store the C password that is confirm password, that is only for validation ok? right so we are done with register right?
as soon as we register we do return render, what do we do? take this inside, return render to our login page, so app slash login.html.
Hope you are able to understand me.
So this is done perfectly and here let's pass a message so that the user gets to know that he has been successfully registered.
‘user register successfully’ and we will send this message on the login page ok? Right?
now this is done, user registration, but what if password and confirm password does not match then we'll create else part.
For that we will pass one more message here which is ‘password and confirm password does not match’
ok and where will we send this back? to our register page with the message.
one more thing If the user is already existing, he'll go directly to the login page.
we will redirect it to the login page directly, ok? right ?
or we can keep that on the register page only ok?
Now what we have to do is, first of all, show the message here on the register.
whatever messages we sent, server side, which means sent through view, we have created a new view.
What we have to do is create one URL.
let's go in here app URL which is here, I created URL, “registered”
Here views dot Register user.
I took the user register, okay? Which is the name of our view.
And here took the name as register okay? Right?
And copied the register name, go to register.html.
Here in its action, pass through the URL in the way we passed in the crud application right?
Now here we want to show messages of server side validation that we sent through view.
These messages that we have sent, also need to be shown here.
So how will we do it?
Suppose here I took the h3 tag okay?
I gave a style to the H3 tag.
Which is like a color : red, I gave the style.
Because We need to show an error.
Here you just have to write “msg”
You just have to do this much.
You don't have to do anything else.
And here we have used the login page as well.
So here we need to create a template for the login page.
For now I am only making login.html okay?
And here in H1 tag, i will write loginPage
Okay?
So that we get to know the user is registered and is on the login page.
Okay? Right? Clear?
I hope you have understood this much process properly.
Let's check this once.
Refresh, our server is closed. That's why that error came.
Let's run the server once.
Refresh here and it came.
Here I will register the user which is s@gmail.com right?
I will add contact here.
And here password okay?
And let's sign up.
So our user signed up as he came onto the login page.
Let's check it out here once by refreshing.
So here is the data.
So see done with sign up.
Our password is also stored, everything is stored.
Right? Perfectly.
Okay now let's try to register another user with the same mail id.
As i took rohit sharma, same mail id okay?
This should validate us right?
Rohit 123, password added.
Signed up and see here it validated us right? Clear?
Did you understand this?
All this is coming left side, let’s bring it in the center.
So here this h3, let's take this in the center tag.
I will cut this center and paste it at last.
And refresh here.
So see here your message is “user already exists” okay?
Now see what we do is, rohit sharma with a new email id this time.
But this time we will not match the password and confirm the password okay?
Here i wrote rohit and here rrr and sign up.
So see the second validation is also here.
So what here is, our server side validation is working perfectly.
And the user is also getting registered perfectly with all the validations.
Right? Clear?
So in this we do user validation with registration.
Right? Clear ?
Now we will meet in the next topic, there we will login user and inside it also we will use server side validation.
So let's meet in the next video.
Thank you.
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.
Share a personalized message with your friends.