Django Tutorial, Complete User Authentication Using a Custom User Model

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we're going to look at how to create a custom user authentication model in django as you already know django is a python framework for web development it's a very powerful and easy to use framework django firewall comes with a built-in user model which has the normal authentication requirement feeds that can be useful when building a small application or a simple website but when building a more advanced application a complex website there may be needs for you to create your own user model to suit your own authentication needs like the normal built in user authentication system django uses the username feed as the login parameters and you agree with me that in modern applications the email feed is preferable as a login parameters so in the course of this tutorial series we are going to be building a complete user authentication system using a custom user model so if you are completely new to django i strongly advise you check out some beginner tutorials on django on youtube or you can head over to the django official website it has a very comprehensive documentation you can read through and it's going to be a series so subscribe so i get updates when i upload subsequent videos so without the most of our time let's head over to our terminal and create our first in django project all right notes before you can work with django you have to have python installed in your system so you don't have python installed you can go to the python docs python official website and download the latest version of python and then python comes with the p package for us to install and python related applications or pattern a framework sorry so once you have python installed you can install a virtual environment on your system by using the peep in stop virtual environment so once you install your vitriol right this will allow us to create future environments in our system or you can use the ppmv if you have a ppmv but we are going to be using vitro environment so once you have that installed i've already had my installed so i'm not going to run this command again so once you've installed python you've installed your virtual environment next is for us to create a virtual environment so to create a future enrollment you can type in your virtual emv they're followed by your environment name so we're going to call this a dj emv all right so this we create our virtual environment for us okay our virtual environment is has been created successfully so to have activate it we simply said into the inf we just created the inside is a folder we have it comes with a folder called script so you see the into script then you use the activate command this height is done on windows i believe in a mark you have to put this in there it's different in mark when activity have you tried remembering mark is kind of different you can just check that out on google and see how she's done all right we have our virtual environment installed already so let's quickly head back to the folder we want to put in our django project okay i have a folder already called then youtube this we are going to put in our jungle project so um first we're gonna install the drinker package by using peep install django so this will install our django for us we start the latest version of django which is 3.0.7 at this moment okay python package has finished installing so we have we've installed python already so what we're going to do is to create a django sorry i said the pattern we start django already sorry about that so next is to create a jingle project to do that we simply write in the type in the django dash admin command start project and the name of our projects can call it a name let's see let's call it custom dash user sorry underscore user okay so this will create a jingle project for us so we can open this up in our code editor i'm using future studio code you can use any other editor the one you prefer all right okay these are django projects you can see the manage.pi and scripts so what next is to create a jingle app so let's cd into the custom let's go user then we create our app by saying python manage the pi which is the jingle script that we just saw then start app let's call this up account you can call it account you can call the user account whatever let me choose okay so this will create the accounts folder so if you check it back in your once you head over to the custom music you can see the account folder which is the app we just created right cool okay so we are having a clean slate of django project and jingle app so we head over to our account app and we head over to the model so for us to create a custom user model we have to import two classes from the django framework which are the abstract base user and the base user manager so we will say from django does contrib that's alt mode to import the astronauts all strands of misspellings commute sorry it's supposed to be modest now mother yeah astra b's user on the base user manager so create our own class you can call it any name you want i will call it my user so which is going to here it's the abstract base user so what this astra business does is it provides us with the core functionalities of a user authentication system like the password the password hashing the session and storing and recognizing sessions on the tokenizing password reset and all those functionalities of your authentication system so the abstract pieces are provided us with those um functionalities so our class is inheriting from these abstract based user class so next we define our feeds the feed one for our his authentication system so i'm going to create the email feed which is going to be modest that he may fit i'm going to have a boss so i'm going to find a bus name for it because to the eb feed or call it email address all right i'm going to define the mars light for it set it to 60 now set it unique property to true true alright so setting any other fields that you want let's say we have company name i'm just trying to think of the customer name company name which is also taking models watch our feed also give it the boss name send it to let's see also set in the mass length i said to 200 let's make it unique because the business seems supposed to be unique okay let's say we have a phone number want a phone number and then these modes the chat feed you said the mouse lens equals to 20 on the all right you can also setting the bosnian information okay and we put in the some people that's feet that are necessary for a jungle and model which is the death joint they join it's gonna be more modern dot daytime in the daytime so we're going to set in the auto and that's called now let's go add equals to true talking about the daily user joins the app or signing to the app you can also set if a bus name for it if you want to setting the bus name equals to all right and then other field you have last login also date time feeder so the same vapors name which is last login so i'm going to set in the auto underscore now to true okay yeah there are some important feed also which is the he said me he's add me he's had me feed yeah more let me just create demo is active i want to define all of the data we have there is staff is super user okay and these are model and these are boolean feeds we're going to set the defaults is that me default is false don't want every user that's hanging so i have to have access to the admin then it's active so we're going to set the default stuff to true so we just send the website into the album because an active member of the site there is staff we don't want a visa to be which has the default also to force and same for you this is a default also first all right so whatever extra feed that you want to put in your user model you can as well define them here but the most um necessary feed that you must add to your model are they dead trend last login is at me he's acting his staff and he's surprised that they are essential to ending his money all right then next what we need to do is to define the feed once you use to login into our app and jiggle calls that's the username underscore feeds which is equals to a string so what is the element that sophie defines defines the feet in a model that wants to use as our login feed so let's say i want to use the email the unlocking feed so then next we also want to define the require feeds it is going to be a list of feeds that we define that wants to we want to be required i want to require of the user we have the company name the every user must provide what the user must provide the company also they must provide their phone number which you defined also just type in all right so these are the feeds that will be required of the user the date drain is provided automatically when user sending stand up i mean login also all these others are provided so these are the feed that is required then the login feeds that will defend we don't need to add you to it because already required automatically so if we define any other feedback like first name last name or whatever field that you want to require the user you added to the require feeds okay so now we're defining our string function which would be the default for the model the default display for the modus wants to return safe dots let's say then company okay okay that's that and next we're going to define some function also like the permission function so we're going to define the function called it has underscore permi what this function of my does talking about if the user that signing up is going to have any permission that we're going to define later why when you are using group to define permission for your user so it's thinking about if the signing user is going to have any kind of permission so this should just return true okay that's for the information then we have another one called has underscore module under the last commodity permissions so what this function is about is if the sign in user signup user is going to have access to other models that we're going to define in this app we are defining example they probably when we are building our app we have other models provide like a product model customer model another so if this user is going to have assets those model so that's what this m has model permission is all about and this is going to take in safe and sorry about that and the app underscore label okay and it's simply going to return true also okay next we're going to create a manager for the my user model so which is also going to be a class so above the my user and class that would define for model will create another class i'm going to call it any name we want by conversion you can use your model name followed by manager all right we're also going to inherit from our base user manager so this manager is going to define the method that's going to create our user we have two methods which is a create underscore user method we're going to take and save our username feed which is the field we defined here the email so it's going to take in the safe then the email and followed by the required fields that we defend also which is going we have the company name and also we have phone and lastly we're going to pass in the password also we should set unknown sorry okay so set some validation check and create some space here what the hell is wrong with future city code all right then we'll check for errors we said if not you mean as if the user did not provide any email and the summary button is clicked we raise a value error with the message that say then email is required and also check if not company or service also the value error say in the company name is required it's also great for if not phone also is an error you can say please provide um an artist from number all right so as as many feed that you passed into your required and feed you also make sure you create a validation check for it all right so what this check is actually saying that if probably the user fee to provide an email if you raise this error or a facebook provider the company name or the phone number which are the required feed and this error should be raised all right another very first step then once we finish that we create our user if everything is all right the user provides everything we create to use up using the safe dot mode so passing our parameters into it which are the email will be equal to safe dot normalize you may what this enormous email does is actually to format our email using must be provided dot com also dots whatever the name will be all right so that's for that and then then we also have the company because it goes to the company i'll put a comma here sorry here we have the phone so it goes to the phone number that was passed into the function all right so that's then your recording save and user that's set underscore password once we pass in the password argument to it so once we pass in the password the awardness is to save the user by saying that that save then using equals to chef.underscore db return the user all right so the create user function takes in the username feed and the required name and require feeds check for their validates them after validating them then creating the user in the model setting the password also that was provided and saving them to the default model which is the model of this class um returning the user so that's for that so next we'll find the crease supply is a function a method whatever you want to call it it can be called method of function we're also going to take in this f email and all the require feed as well make sure you have the class in the password also just like the way we did it in the queso function okay so we're going to create the super user and create a variable called user set equals to save does create user so recording the create user function that will define the befores which we're going to pass in the required feeds which is the email so this advisor should provide an email also provided company name and phone number so these are the feed study and that's my password what am i doing sorry because all right so the surprise is going to provide these also and also it's going to provide the for the supplies i also want to check change the function of the value of the add me set it to true because all supplies has had me and they also change user dots is a super user so it's also too true so we're going to save the user is that that safe also using equals to save dot underscore tv and we also return the user all right okay let's start off for all right so what we do next is we have to tear this and model the manager so i have to point the manager to this model by setting the object object equals to the manager we just created by saying my user manager okay so the model knows the manager is his own manager which is his management just defined all right that's all for the user model less i forget we've now added the app that we created which is the account and our settings so head over to our settings and then go to our installed apps and add the account app created alright and save this and leave here okay next we'll make our migrations sorry for less i forget before making migrations we have to let jingle know that we are no longer using the default user model but our customers are monday so we'll head over to our settings and in our settings we'll set the odds underscore model you can set anywhere less than a year we set the value for odds let's call mode sorry underscore user model i will set it on to the app name followed by the model we just created which is called my user so this is very important don't forget this i'm also going to do it before run migration make sure you point to the user model before running your migration so this should be string so we are setting the alt underscore example the equals to this model we just created so if you fail to do this your code will not work because you will not know that there is a custom user mode so you still use the default one so once you do that and make sure we save it all right so when we run our migration let me double check it gets okay i think everything's okay so next we have about two terminal i'll run our migration back right battle manager by make migration make migration okay say creating more than my user so next we run the python manage dot by migrate well that's good so i've migrated already so we can create our super user now by also running the python manage let's buy create super first to enter our email just where we defended defined our feed so say email let's say email ads and admin at gmail.com he asked for company name which was the feed also defined so let's call the company let's say aries aries rt solution company's phone number we can write any phone number this is not my phone number just writing some random numbers okay then password so django will not show you the password but it's actually working and then you see so super user created successfully that means our support user is created successfully so let's clear on uh apps by saying name python managed by run server okay upper started network browser and run this part open this button head over to our admin we'll see the model we just created but won't see that until we add it to our model to our admin so here we have to import imports from accounts dot model after import in my user model there we have to register it by saying add me dot sites dot register passing in demand me my user here so save this and then dive out to our terminal let's quickly try creator because we've not created any view so we show the default and django home page so we add a header bar to the admin then we can sign up for the email we just created which is add me at gmail.com that's the admin administrator then we have them password i enter the password please enter a correct email and password for his staff account and that buzzfeed may be case sensitive let me enter again let me add jimmy gmail.com i guess that's the feed i entered yes add me at gma.com all right then the password okay i'm getting some errors let's quickly let me quickly debug our code and i went back to the code and i went through it and we didn't set the is underscore our staff to true i believe that maybe because of the error so i turned that to true and also the email for the create uh super user we didn't normalize it before so i normalized that email then that's all i did so i saved it so first to do that we have to delete our initial migration that we made so head over to the migration file and folder here and then this initial migration delete it delete initial migration then the database file delete it that's like also we head over again to our terminal and re-migrate the creating pattern okay you see better okay then we create our supply user key the same pattern okay protostarter an image who's here at me that's company that's called company name let's keep okay surprise i created successful item managed one more time all right let's enter the email which is at me at company dot com then password all right we are logged in you can see the my user model we just created which is the account in the account app if you enter now you can see the the name this is why we are getting the name of the company because we set the string parameter here the string function i mean to be saved as company name that's how you're getting the company all right i'll end this video here we'll continue from uh next video we have to like customize the admin then create a registration view alright so subscribe and hit the notification button so that you get notified when i upload the next video
Info
Channel: Henry Coding stack
Views: 9,156
Rating: 4.652174 out of 5
Keywords:
Id: Y-JNrQli2C4
Channel Id: undefined
Length: 40min 21sec (2421 seconds)
Published: Mon Jul 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.