Django Authentication - User Registration , login and logout

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome friends welcome to bill learning club in this tutorial i will show you how can you create a registration from a login form and log out you can create the form by using the bootstrap so you can just click on register so you have to fill the all fields first name last name email username password confirm password then you can register then after register you can log in so here just put the username i already create a registration for play e-l-a-y and password is claimed now you can log in so here you can see your login the username is play first name is pleasant and last name is jensen okay so now just go to the admin and now you don't have any permission to anything because you are a staff user so you have to create a super user i already have created a super user so just log out so log in again and just type admin this is the super user and password is admin and the login so now just go to the user and you can see here these three are the users admin it is a super user and staff status is okay the clay it is a normal staff user the staff status okay but ravi it is not a stop user so i will explain everything about the staff user not stop user and i will explain about register login and log out and it you will get a free pdf of these sessions don't worry this pdf is absolutely free the top two button all are here this is the exclusive content i have created you don't need to pay anything so i will put this into the telegram group just join i give the link in the description box i will also give the source code in the telegram group don't need to worry just watch this video and make make this project after watching this video and don't copy and paste all of these things from here i just give you this pdf and source code but don't copy i recommend it so i'm just watching the video from start to end and you will know a lot of things here so this is basically about registration form login form and log out okay so let's start so at first i will create a django project and django application so just open cmd in this directory and now just create a django project type django admin start http start project and what is the project name the project name is project project okay so now change directory uh project project and inside the project just create a django application so just type django admin start app and what is the name of the app here i use app app so app is the name of my django applications so now open it with any code editor so here i use vs code and now just at first go to the settings inside the project and now just go to the install app here i have to install the app app app is my application name and now just go to urls.py here just include the url so here just paste it and now just remove it include app.urls app app dot url and now just import the include module and now go to the application and create urls dot py so just go to the app and create urls dot py now now go to the urls inside the project from here and just copy all things and here just paste it and just remove it and at first i will create a home page so for the home page i use here the home function so just go to the views and just create home function and what define home and request and then return re t u r n return render request and render to home dot html now go to the app folder inside the app folder you have to create a folder named templates so just type templates templates so inside templates folder just create home dot html so just type home dot html go home dot html now save it save it save it and now go to url start by here just you have to import the home function so just type from dot views import home function this function and now just change it this is my landing page and now just type home and the namespace of this url is home and now save it and i'll go to view sort by and now go to home.html now go to bootstrap and go to the docs and now here you can see include bootstrap css and js just copy this and paste it here and change it to b [Music] now change it to auth by user okay so now you have to run the server actually just check it everything is okay or not python manage dot ui run star part and hit enter and okay so just run the local host yes it's absolutely fine so now just close it and just make migration and migrate python manage dot e y make my creation i have no models inside models dot p white so this is not necessary and it will show sorry management energy managed by make migration did you mean okay okay i just did a spelling mistake so i just copy that and paste it here and type make migration and no changes detected because i don't have any models inside models dot pi so just directly you can migrate so i'll just go make my grid my uh python manage dot py migrate and now migrate migrating all the models okay now create the super user so just create the super user and username is admin and email address admin address gmail.com and password is admin and this is admin yes okay create a super user has been created successfully so now just go to the home.html and here just add a class text center and text success and now go to the bootstrap here just search for a navbar so where is the name part yeah this is the number just search for a nap bar okay i just copy this code this step up just copy this from here and paste it and now bg dark i always prefer dark napper bg dot and nav bar dot okay now save it and run the server python manage dot mini ui run stop and the server is running now and now just replace it okay so it's working so now what's next is create register form by user model so what is the user model at first the user model is from the django.contrib.org dot models and there i can import the user model so you have to import this user model from django.contrib.org dot models just go to views here import the user model uh from django.contrib.org.model and now uh define a function for the register so just define the function d e f r e rage start and now just pass the request and just type if request dot method equal post i just copied from here simply i will explain what is it so i just copied from here it request method called post so at first type if request dot method equal post that means uh if the method in html form it is post or gate so is this method is boost then fast name equal request dot post then first name then the first name it will get the first name from the form in html i will create it later but i just clear define this function now and now now i just copied this e password equal confirm password then it will save otherwise it will not stay so actually i just create a register.html at first and you can easily understand otherwise it is hard to understand so at first just go to the views dot py and type here else block else if request method is not post then return and then render and then pass the request and then just type the html register dot html so now create register.html inside templates folder okay so now i just copy this thing from here and paste it from here and just go to bootstrap and search for a form so where is the form where is the form i just searched for a form from a bootstrap and where is the form man hey yes this is the form okay so now search our suitable form from here okay so this form is very suitable so i just copied this and now paste it here so before paste just create uh create a div tag with uh class name uh content fluid on then if in uib and paste here the code and this is basically so now just go to urls.py here just import the register and now create a path for the register so just i'll copy it and paste it here and the function name is register and the path is which we start left and namespace is also crazy stuff and now save it and now go to views dot py and it render to register.html so this is the register.html and now save it and now it can't import name register from f dot view okay so this is a return render request render request register.html and url here register yeah absolutely fine i just remove this at first so i just comment out this thing and full effects block okay so now save it and run the server return render and html okay it is absolutely fine man why it's wrong now save it and run the server now go to the register yeah this is the form actually so here i will use the first name last name email password so just create that first form so act first first name first if ir is the fast name and type is not email type is text then this copies this and here just paste it and this is the last name last name type is text and just remove the id email so now after this i need email field so just go to the bootstrap and here this is the email field i just copied it and now paste it basically i create a form here now email here type of the email now after email i will create the username so just copy this thing and this is the email after email just create username so here type user name and after user name just create a password field yeah it's already and now after password field just create a confirm password page confirm password now remove all of these three things okay so this is the button do not don't remove it this is the button and now yeah it's actually fine now save it and now just replace it uh first name last name email you username password this is password this is not possible this confirm password so just type on password and now this just type g start and tie button and here add a class bt in large button large and button success btn is ucc button success i prefer button success so now again we press it okay so first name last name email and user name password and confirm password all are here so now the main important thing is the name field in fg input tag so just type any name name and just give it fast name if i r is the first underscore name name first name and from this in input field it will take the first name that means this so they that's why the name field is very important so now just go to register.html name is the first name and now i just copy it and for the last name just put name as last name and now this is the email here inside the input field this boot name is e n a i l email and now for the user for the username just book name equal user name and for the password just put name equal password and for the confirm password just move name equal what one from password so just go to views dot py name equal confirm password okay so just copy each and star.html just type quantum password inside the confirm password input tag so it's absolutely fine so now this is basically post method so just type method what is the method the method is post p o hd post and now type the action action is what action just type person person url and just type register i made a video yesterday about this so this is the namespace register go to urls.pi inside application folder so this is the name space for the register and now in django this is the post method so i have to use csrf token here so just type percent percent is r if underscore token srf token okay form tag is ready absolutely ready so now just replace it okay so now go to use dot pi so view stock by here i just remove these comments um and at first just type if request dot method equal post then first name first name is what first name request dot post ah this is the method post actually request.post and is the first name and this is the and this first name will get the value from here here is a fast name this input field so very careful about this this name tag is very important so just this is the first name this is the last name this is the email this is the user name and this is the password and this is the confirm password so now go to views at first create a variable first name this will take the first name from the form and then create a variable last name this will take the last name from the html form and then create a variable username this will take the username from the html form and then create a variable name email and this will take the email id from the html form and same way for password and confirm password okay so now if this condition is not run then else block will execute so here i just built this is for only understanding clean statement otherwise you don't don't type this so these is not post method inside the else block so after this if password equal confirm password then user that mean then it will register otherwise not so i just copied this thing copied this thing and now paste it here i will explain don't worry i will explain each and everything so after uh this all just type if password equal confirm password that means a password value or password match with the confirm password then user.objects.builder what is the user user i already imported from auth models user.object.builder user equal username dot exist if username is exist then message.info okay so you have to import the messages also this is the django messages here i used to just from i think django dot shortcuts no no it is not django dot shortcuts just let me check it this is form django.contrib okay so just type from django dot contrib input messages so if user.object.filter username equal username that means if there is a same username then it will not then it will uh this is not email dc it will not create that means it will not register user name is already exists is already exist okay so username is already exist it will show a message and you have to also import the redirect from django.shortcut so just import the redirect so if the username is match then redirect to register that means this function and otherwise user equal user dot object dot create user now if it is not match user name is not match then i will create a user so you can also do the same with email address i just create i just do it with username you can do it with email address so if username is not exist then username is not exist then use the create a variable user user equal to user.object.createuser and now type username equal username this all thing password equal to password email equal to email and first name equal first name last name equal last name actually this orange color username password email first name this all are the fields inside the user model and this green color username password email this actually the variable of this and this variable takes the all value from request.post and now user.objects.createuser from this value username called username password equal to password username is a field and username is the variable username is the field username is this username is variable this password is the field this password is the variable this green color password is the variable and this email is appealed inside the user model and this email is a variable this okay so now after this you have to set password very remember friends this is very important user dot set password and just pass the password and this variable remember you have to pass the this password variable so just pass the set password pass pass this password variable then user dot save then print success you can use it or not it's up to you and then redirect to login user i will create the login user i just remove it print print statement actually you you don't need to use but but for the developing it will help where the code is execute or not it will help so that's why i create in statement so just save it but i don't have created any login user so just go to the urls.pipe go to the app url dot by here i will create a login user so i just copy this thing and paste it here so just focus on this this thing path login user and just this is the function login user i will create it so just go to view and now here just create a function for login login underscore user and pass request and now just return reindeer not redirect return and render and just pass request and just type login dot html okay so now save it and now just go to url start by here import the login user function and namespace of this url is login user okay now go to the view start file here after registered successfully it will redirect to login user that means this function so now you have to create a login.html inside templates folder so just uh go to the templates and create a folder name login.htmlogin.html so from home i just copy all of these things and now go to login and paste it here just cop copied all of this thing and paste it here i actually i just copied from i'm registered this is the register.html this is login function inside the pdf you can handle all of this thing oh my god now one thing more you have to uh where the not views let me check uh this is the messages so one more thing i just copy these messages so inside the views dot py here i use the django messages so just go to register.html and after form tag just type uh deep class and just type for message in messages and this is the django messages m-e-s-s-a-g-e-s that means this and or message in messages and you can create a class enlarge danger it will background is read and this is the message that makes this basically this is a follow for x in y print x and print the all of messages okay i think you know about general message and now go to views.pi so this is the login user function and now this is the login html so i have to copy here and now just paste it and i'll be learning the login page login page we learned include login page now just go to the bootstrap and now choose a form for login so this is the first name last name i just want to a button with first name last name and this is perfect this is perfect copy and after h1 just create a container fluid class on a container under um build api not understood and now paste all of this thing so this is the submit button and this deep tag for the check box i just removed this this is the field set also also remove this field set i don't need this field set and just save it and go to views and this is the login user log into html and go to urls okay just copy login user and now just run the server and here just paste the path login user okay email and password successfully so now just go to the login.html and now here this is the email and is the password but here i use the username user name and type is text just to remove the id and now how here is also important is name name build his username is a username and inside the password input text just type name password okay now here button log okay so now just save it now go to the login function so this is the login user function login html i just copy this thing i will explain it don't worry i'll explain each and everything so now create a if block if request dot method equal post and then username it will take the your username from the from the login.html here this name field and then password it will take the password value and then create a variable name user and user then type auth dot authenticated you have to import the auth so i already imported now import auth dot authenticated user equal username user this for this orange color username is the field this the green color is username is the variable this and the password and equal password so then type if user is not none then auth dot login request comma user and this is the variable user and then after return redirect home directly redirect to this page and if it is not work that means if user is not none then just type a message message dot info this is the django message request invalid username or password and then redirect to again login user page this page so login user is a namespace you can use it here and now render and here just create the else block for this block wheels and now just paste the game space here and now save it okay this absolutely fine and i have successfully created the login page now for log out just go to urls.py just create a path for logout user and now just go to views and here just create a function for logout user just copy it from here this is very easy man the logger define logout user and just pass request then auth dot logout auth already imported and log out request and then return redirect to home you can redirect any of the page you can create so now save it and now go to the url start pipe now here at first import the logout user model and now this is logout user and namespace namespace is logout user okay it's absolutely fine now the main thing is if user is authenticated so that means go to register.html so just go to register.html where is the register.html this is views.com this is not register.html ok i will correct this here it is not registered dot html this is actually these functions replicate so just for this you have to create if request dot user is authenticated if request request user is authenticated that means you are logged in okay so just copy this and go to the login i will explain everything don't worry so after the learning club log after be learning club text just paste it so inside the login page so just type if request dot user is authenticated then you are logged in and if then the user name is username and otherwise user is not logged in so i am now not login so this will show inside login and just run the server so just refresh it you are not user is not logging you can see it username is not login and you can add the class here h3 uh sorry h3 not hdist actually i am i'm recording this video at midnight so that's why user is not logging okay now user is not logging you have to log in from here so i just write admin admin this is the screen okay so now it will not work because what because here the form the form action is not defined anything is not defined method is not defined so just type action just die you url so just type past change percentage and then url and what is the namespace of the login this is the namespace login user just paste the now method method obviously post and this login it will redirect to login user and if you are authenticated then then request.user.username this block will run so just save it and run it and now replace it login user and now type admin yeah just type admin csr verification so here is the form and now just type csrf token c is our rate underscore token and now save it and now just replace it and just type admin admin is a super user i already have created now login okay so i am logged in user is authenticated user dot user name you are logging this is the home page and these views it will redirect to login dot login user no it will redirect to home or okay okay it's absolutely fine so just go to login and i just copy this block and go to home dot html and after the learning club text paste it so if request.user is dot is authenticated then just create a para tag you are logged in and just create another paragraph username equal request dot user dot username this is the django variable request dot insert document and just end this if block sorry if just create another else block not end here you have to end this if block so just inside the else block that means if user is not authenticated this block will run in home dot html so if i refresh it that you see you are logged in and the user name is add so just it is not inside the container fluid so just type a class container nfluid so paste this block if request dot user is authenticated so inside the class just leave a tab not that this is the class name h3 and h3 okay so now save it and now just replace it you are locked in if you logged in you don't need to register so you need only the logout so just uh you are logged in now so you need the uh you need a logout button so i just copy a button from here okay so i just copy this thing you are not registered this is basically about contact and now go to home.html if you are not login then you need to register or shortly you need to register so i just copy the button and now paste it so here just give a anchor tag and url is registered the register is the name space actually this is the namespace in urls.ui type is button plus this is the bootstrap class and register actually it will redirect into the register page so i am now logged in so that's why this block will not run this block will run because i am logging so i need a log i need to log out so i just copied this and paste it here and i need to log out so just create a logout log out and now what is the namespace for the logout go to urls.com namespace for the logout user is about user just copy it and go to on.html and here just change it to logout user and if you are logged out then this button for register and this button for logout i now need to save it save it so just run the local post and now you can see the log out button just click on log out button user is not login okay so that means what that means this block is run and now this is the register and now if you click on register this is the register page and you have to fill the form okay so i think absolutely fine so here just put just uh fill this form what is the first name the first name is ravi gmail.com user name is adavi password is ravi and confirm password is ruby and save it so so okay it's uh it will redirect to login page user is not login now just type ravi username and the password password is what and now click on login okay you are now logging and the username is ravi but i want to print here the first name last name also inside the home html so if user is authenticated then i also print the first name and last name of the user so here just first name ir is the fast name and last name so just change it to la is the last underscore name actually this is the field last name i just show you this this orange color last one and now just go to the home.html and now where now this is the username i think where is this okay i create it in login.html okay don't worry so just this is the first name past underscore name first name and now just copy this thing and just paste it inside home so this is the request.user.user and after that just paste it the first name and the last name and logout user if you are logged in and if you are not logging then here is the register and just create a button for login also in the home page first what is the namespace for the login i think login user so just go to ui instructor so this is the namespace copy it and go home and login user the name of the button is log now everything is fine so just save it and now you can see the first name is ruby and last name is right first name and this is a success i just info and info this is sign color basically nothing but username first name and actually this is in login.html i have to do in home.html so here class access just change it to info and just change it to info don't worry about the source code and you will get everything the pdf the source code you will get everything just join the telegram group you will get everything for free you don't have to pay anything so now just save it and just add a cl class here page 4 is the heading tag for h4 so now save it and run it okay so first name is ravi last name is right username is ravi you are logging now click on logout so user is not logging you can directly go to login or you can directly go to register for new registration or just you can go to login again here just type ravi and rafi ravi just click on drama so now okay it's fine now just go to admin class admin but this is not a super user you are authenticated as ruby but are not author authorized to access this page would you like to log in a different account so you have to type here super user or staff status so just type admin super user admin and and just admin and login and go to user and here you can see the ruby but staff status is closed that's why you cannot access the django admin page so you can just click on ravi so you can see the password save is in has algorithm this is very very important any nobody can see the password in database so you can also on the staff status so just go to view start pipe here before the save just type user dot underscore is in the stop so this is the name staff staff equal binary value true we are true okay now save it so then the start status will okay then you can access the admin page when you create the register so now log out and just go to the home page and now go to the register play in a right click and last name jensen did do you remember this is that basically uh wave series and name it reason why by jaisa so clay johnson and now give a email address and here just keep user name play and the password system adds confirm passwords by click and register okay so now user did not login now you can login cl a white clay and cpl a white clay logging okay so you are locked now click on log out now go to the admin so after log out just go to the admin so here you can type also play c l a y clay and here you can also type the play but here in the login i just type c right click and password is play and login now i go to the admin panel so here is nothing to so because you are not a super user you are only a staff user so just go to the just log out and log in again now add me admin this is the super user just login and now go to the user and here you can see the clay the staff status is on and for ruby the staff status is off that's why we cannot access the admin page but they access the admin page but they don't have any permissions play don't have any permission to change anything in admin page so only admin has the permission because admin is the super user remember so basically this is the authentication system in django by user you can create it easily you can access the pdf you can access the full source code just to join the telegram group link is in description box so this is a very good project for beginners and all create a register page a login page by user and one more thing inside the view stock ui user.set password is very important it will set the password as a hash value if you don't use this that the password is will be not set so for this reason password set as an hash value so i will put the all source code here in the telegram group and also i will give this pdf absolutely free thank you for watching the tutorial guys and keep supporting this channel keep supporting [Music] the content it will take a lot of time to make the pdf and to make a project so just keep supporting and thank you the pdf is absolutely free you don't need to pay anything just join the telegram group you will access the source code and as well as pdf so thank you bye friends good night [Music] you
Info
Channel: B Learning Club
Views: 13,503
Rating: undefined out of 5
Keywords: Django Authentication, User Registration, login, logout, django login and registration, django login authentication, django register and login user, django register and login, django custom user registration and login, django register user, django user login, django user model, django login, django bootstrap forms, login and logout by using bootstrap form
Id: HyfXajKBIo4
Channel Id: undefined
Length: 63min 10sec (3790 seconds)
Published: Sat Jun 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.