User registration and authentication in Django

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] and welcome to the next Amazon activity which is going to be focused on handling user creation or registration and authentication in Django now of course I will be sure to chapter this video accordingly so for those of you with some experience you're more than welcome to skip ahead to the relevant section and for those of you that just want to watch from scratch or just want to follow along you're more than welcome to do so you can just follow this I do another thing that I want to mention is when I start and set up a Django project I follow a particular um way of doing a particular routine now if there's something that you prefer so something else that you prefer your own style to doing so feel free to go ahead and choose whichever method you feel most comfortable with okay so you can decide how you can go ahead and set things up but what I like to do is I usually just create a folder on my desktop so I'm going to say new folder call it Dev okay this is where I like to Hales all my Django projects and the next thing that I do is I head on over to the search bar and look for the CMD so the command prompt okay it will usually say my name there as a user and I just want to resize this now you can also use the visual studio code terminal um the reason I use the CMD is just because of habit and I just feel it's a bit easier to just minimize and maximize so here we are so let's clean this up okay so now that I'm in my command prompt I want to head on over to my Dev directory so I'm just going to say CD desktop and now I'm on the desktop then I can say CD Dev so for those wondering what CD stands for it means change directory so I'm moving to another location so to another folder so now I'm going to say CD div and now I'm in my Dev directory and we can clear this up so now I'm in Dev and we can go ahead and proceed now the next thing that we should do is install the virtual EnV package so I'm going to say pip install virtual EnV and this is going to allow us to create virtual environments now this is one of the few cases in which we will install a package globally on our system as best practice you don't want to install packages globally in your system you want to create a virtual environment but in our case we have no choice we have to install the virtual EnV package globally before we can start creating a virtual environment so now what we can do is we can just say virtual EnV venv now the venv here is the name that you assign your virtual environment um so I'm calling it v e and V you can call it e e EnV or you can give it any name here so this part is customizable the command is virtue and V this needs to be used and this is the name that's custom to be your virtual environment folder and you can press enter okay so that's going to take a moment okay there we go quicker than I saw now you want to activate your virtual environment so let's clear this up so CLS just clears the screen for us to reduce all that clutter so I'm going to say VNV backslash scripts backslash activate and that's going to activate the virtual environment and here we can see it's activated within parentheses so whichever name you assigned your virtual environment is now going to be shown here on the left side okay with uh parentheses on the left and the right now if you want to deactivate it you can run the same command but instead of activate you can just say deactivate and you'll see now that that parenthesis is gone so now you are on your global system so whenever you are running your project and whenever you're installing packages most importantly you want to make sure that you run the following script and that you're within your virtual environment as follows so it's best to just keep this on rather it's the best practice so now that we are in this virtual environment we can install Django so now we can say pip install Django okay so let's just install that okay so that's going to take a moment so what I'll do is I'll pause the video and once that's been completed we can continue with the process all right so welcome back so now we can see Django has been installed and we are good to go all right so now we're in our div directory so if I were to minimize this and open up my div folder I can now see that I have a virtual environment folder here it's sitting here and ready to go now we can create our Django project so what we can do is head on over to the command prompt and here I'm going to say Django Dash admin start project and now you want to give your project a name I'm just going to call it elevate as an example you can name it anything that you wish and we can just set that up there we go so we've created our project Elevate now we can just say CD elevate and now we are within our Django project and just for um continuity you can open up your folder and you can now see that our Django project has been created and we are now currently within that now okay so next thing we want to do is we want to go ahead and utilize a text editor or an IDE for that example so I'm going to use Visual Studio code so I will be sure to attach a link in the description below where you can install Visual Studio code and you can use anything anything that you prefer so any sort of text editor would work okay this is just a suggestion and sometimes the folder doesn't automatically show up here so what you can do is you can say file open folder and you can go to your desktop wherever your directory is so mine is Dev here on the directory and select that folder and it's going to open up there we go okay so I'm just going to zoom in here so we can actually see okay there we go all right so the next thing you want to do is we just want to look into our folder so we have our Elevate folder we have our manage.pi file we have our settings.par file we have all of the basics here all right so we've got that set now of course this isn't a beginner tutorial on Django so I do have other content on the channel on that where I go into deeper um I go I'll give you a better understanding in those videos okay where I explore the fundamentals of Django okay but just make sure you've got everything set here and ready to go so you have your Django project and you have your virtual environment folder okay great so what we can do now is we want to just run python manage dot Pi run server okay so let's do that okay there we go so we can now see We've ran our server and we need to make our migrations and we're using the latest djangles version as of today now if I open up my Django project we can now see we have a default sqlite database so by default Django uses an sqlite database and we just want to make sure everything's connected fine so we can go to localhost and of course I already have that here in the URL and there we go we can see our Django installation has been set up correctly so we've done everything that we needed to do in order to create a Django project perfect now the next thing you want to do is create a Django app okay so what I'm going to do here is stop my server and before I create that Django app what I'm going to do is just make those migrations so I'm just going to say python manage dot Pi all right great all right there we go perfect okay and now what we can do since we made our migrations we can create our Django app so it's going to be Django Dash admin start app and I'm going to give my app a name I'm going to call it my let's say uh what should we call this we can call it CRM something very simple so I'm going to call my app CRM you can give it any name that you wish clear that up and now that we have that set we can head on over to our directory and we can see here that we have CRM okay so now we need to register this app in our settings.pi file so we can go to our project settings.pi and what you want to do is you just want to scroll down until you see there's a list of installed apps and you just want to add in the app that you just created so in my case it is CRM so here I just want to put in CRM and add in a comma there at the end okay so we've now connected that now it's good practice to just run your server again just to make sure that you aren't running into any errors or whatnot okay that's fine and refresh okay we're good so make sure now that you've created a Django app and that you have set it up accordingly right so what we can do now is we can go ahead and continue so now what I want us to do is to create and connect our uls.pi file in our views.pi file so essentially here in our CRM okay we want to set up our URLs from our app here we don't want to set up our URLs in our main uls.pi file here in our Django project so what you can do is open up your app and right click and you want to say new file and we want to create a URLs dot Pi file so you can create that here so this urls.pi file and we can just add in some lines now what I want you to do is to keep this open and we can open up our project here Elevate and you want to open up this urls.pi file here so we can see that this urls.pafer is from our app CRM and this is our main urls.part file in our main Django project and you can see we have a lot of comments here let's just remove this and of course we can see this is where the base of our URL is going to be situated such as our admin URL and all of that so the first thing I want us to do is just add in some logic to urls.pi so we can just copy all of this here from our main project urls.pi file and paste it in here and we're not going to have this um URL and we can just keep the pass function here for now and this URL patterns list so you just want to add that in to your urls.pi file which is situated within CRM you want to add that in okay so the next thing we need to do is we need to connect our urls.par file from CRM to our main Django projects urls.power file the one where the admin is so we just want to connect it because all the URLs we create will be in our app here so in the app level itself so to do that all you need to do is go to elevate and here you want to say comma include so we want need to use the include function to do this and here we can just say Pass open the parentheses and the route we're just going to leave on the default and here we want to say include so we're telling the main Django urls.pi file to include all of the files all the URLs that are set in the links app so here we have sorry the CRM app so CRM Dot URLs so we're telling Django to include all of that so you just need to add in a comma at the end so now we'll be able to create our URLs in this file here without any issues or problems Okay so we've got that set up and ready to go so once you've done that we can close this file for good and now we just want to go ahead and create simple URLs and Views so now what we can do is we can just open this views dot profile and put it here to the right and you want to be using the urls.powerfar from CRM as well great so now what we can do is just go ahead and set everything up so we're going to create a few views here so let's get started with that so first we're going to have a home page then we want to have a page for our users to register a page for our users to log in and also a page as for users to visit once they've logged in like a dashboard page so I'm going to say deaf home page we're going to pass in the request object and for now we're just going to use the pass keyword okay so this path keyword here is going to allow us to have a view and not have any logical functionality in it so it's a very useful keyword to use um if you just want to essentially go ahead and ensure that you have views in place but you don't have any errors when you are trying to run your Django application next we want to have a register view so I'm going to call so remember these are functions that we're setting for our views then we need to pass in the request object and we're just going to say pass for now of course next we want our users to be able to log in so I'm going to say deaf my underscore login is the function name pass in the request so this request object is going to help us if we want to render our HTML templates or if we want to return a HTTP response so it's very important to have this request object in place okay so just add in some space and then we need one more view here which I'm going to set as Def and we can call this dashboard and we can pass in the request object there okay so we have our four views so make sure you've created these four views so a home page view uh register view uh my login View and a dashboard View great we can go to uls.pi and now we can just set them up accordingly so we need to use the pass function which we are importing here and each pass function essentially on this line here is going to represent a URL of sorts so first we need to put in the route name so I'm going to leave this blank because that's going to be the home page then I need to import that view so in order to do that what we need to do is to import this views.pi file that we have here we need to just below pass we can say from dot um we can say from Dot and we want to say import no that's not it so we need to go ahead and refer to it as dot import views yeah there we go excuse me there okay so we've got views now and of course it's going to be our home page Okay so we've got that set and what you can also do now is if you want to refer to it by the name attribute you can just put in name and I always like to keep it a double quotes So that is one URL that's going to be set to our home page now we need to create uh three more so it's going to be the same thing here okay so now what we need to do is we need to set it to register my login and dashboard so I'm just going to switch this up here so register and we can keep the route name here as register so this is how we can refer to it in our URL and then the name okay so the name parameter that we're using here is very helpful if we have links and we just want to switch over to it by Links and these links we can set in our templates okay then we have my login and we can switch that to the view which was my underscore login so that is what we set right here okay and we can just say my login so the name parameter value I always keep the same as the route name this is just a preference you can decide how you want to do it and the last one is dashboard okay we can see dashboard dashboard there we go all right so what we want to do now is we're going to keep it really simple we just want to return a basic HTTP response here for the home page just to make sure that we've configured everything else here accordingly and in place right so to make use of the HTTP response request what we need to do is first import it from Django dot HTTP so from the django.http package we want to import the function HTTP response okay so now what we can do is in the home page here we can just go ahead and set this up accordingly so we can return a HTTP response so I'm going to say return HTTP response and here within quotes we can just say this is the home page as an example that's all you want to do then you want to go to your server you can just press Ctrl C okay and then you want to run python manage dot pyron server now I do want to note here that if you have the server running the whole time while we made these changes you're going to see a bunch errors that's why I say it's important to press Ctrl C so that you don't see them so it's always a good practice if you're working on your views or your URLs make sure you stop your server or else you're going to see a bunch of Errors now this doesn't mean you have errors in your application okay this is just all the changes that we are making and sometimes the server doesn't keep up until we have finished with all of our code okay so we can anyway run python manage dot Pi run server now technically speaking we should be able to run this without any problems there we go and we should see this HTTP response this is the home page on our home page and we can see here the route will be the default route here so technically speaking if we go back here this is our home page and we refresh we can see it says this is the home page and this is also a good indication that we've set up our UIL and our views correctly so make sure that you can see this and you're up to here so far if you had an error of some kind I would recommend going through the code again but just for clarity to help you guys I will just um pause on this screen for a moment so here you can see we have our urls.pi file we've got our four URL so the home page the registration page the login page and the dashboard page and then in our views as you can see here we are going to eventually make use of the render function but for now we're just testing everything out from the HTTP response and this is what we're seeing here in our view returning a HTTP response to see this page right here then of course we have three other views here so we have our register view our login View and our dashboard view which correlates to the URLs here as we are importing it accordingly now the reason that we're just putting the dot there to import the views is because the views.pi files in the same directory of the urls.pi so you can see that in the same directory and we can of course see we're just referencing that accordingly now the name parameter like I mentioned before is just used if in our template we want to refer to a particular route so that's why I always set the name and the same as the route name so the first parameter that we set here is the route name the last one of course is going to be set to the name okay so that's all you need to do make sure you set that up and if for some reason you didn't set up your main project URL correctly I'll just go on back there so you can see and all you needed to do was make use of the include function and include your your app name dot URL so this is going to make sure that we can actually work with and render out these URLs from our app so my app is called CRM and that's how I am outputting it now of course like I mentioned before I do have a video um on the basics of Django where I go to a deep focus so that's something else that you can check out I just want to mention that again so that you're aware okay so that's that so we can just close these up okay so we can see our produce directory at this moment so we have our app CRM we have our project Elevate now the next thing you want to do is instead of returning HTTP responses we want to actually make use of templates so what we can do now is head on to our app CRM and I'm going to right click and what I'm going to do is I'm going to create a new folder and I'm going to call this templates okay and what's best practice is to create another folder in your templates directory and you should name that folder the same as your app name this is to ensure we don't have any namespace errors so I'm going to right click in templates and I'm going to say new folder and I'm going to call it CRM okay and in CRM here this is where we're going to actually create those templates so we need four we need a home page we need a registration page a dashboard page and a login page so I'm going to right click on CRM say new file and I'm going to create four files so index.html right click on CRM again new file and next is going to be register dot HTML right and right click on CRM new file and we can call this my Dash login.html right click on CRM and next we can have dashboard dot HTML okay so let's just add some text here to them so let's say under index.html I'm just going to add HTML and I'm going to keep it very light and very simple here so I'm going to say this is the home page copy that go to register replace that and I'm just going to say um register now okay my login let's say login here dashboard and just say this is my dashboard okay so I've just added in some simple HTML markup so the HTML tags here and just a simple H1 tag for a heading so the dashboard page so this is my dashboard my login page Log in here my registration page register now and my index.html page this is the home page so make sure you just added in some text to your templates so now what we can do now that we have that let's just see the location of it so in CRM templates and in the CRM folder here that we created the extra folder we have dashboard the HTML index.html my login the HTML register of the HTML now we can go to our views and we can just set that up accordingly so let's start off with the home page here so let's just move this here okay so for the home page now what we can do is remove this and we can just say now return and we're going to say render now and now we need to pass in our request object here and now you need to reference it very carefully so templates automatically is going to be recognized by a Django so you don't need to put reference the templates folder okay however you do need to reference CRM and then that name of that particular HTML template so in this example we're just going to say CRM forward slash index.html okay and that's all you need to put in for now so that's it now you can make sure that you have your server running you can head on back refresh the page and now we can see we are rendering our HTML template if you want to double check you can always right click and say view page source and you can see if you zoom in here you can see those HTML tags being rendered on the page just as extra verification okay so that is oh goodness okay uh that is how um you can see it all right so we've got that set now we can just set up the templates for our register view login View and our dashboard view we can just set that up so let's go ahead and do that so we just need to reference it exactly by the name of the HTML template so make sure that you set it exactly as the name here okay so what I can do is actually do it with you so register we can say return let's actually make this easier just copy that logic and instead of index you just want to say register and that pertains to this here right next we have login so we can just put in that logic here and it's my Dash login okay then dashboard would be the last one so dashboard there we go so let's just check them so make sure you've added in um that you're rendering out your index uh the HTML file your register HTML file my login.html5 and dashboard.html4 make sure you're rendering them out okay so now technically if we go to the URLs we can go ahead and check all these templates by going to the routes of those particular URLs so see each of these URL paths as a URL so this is a URL this is the URL this is the URL and we can get to that URL of that pass by whatever is entered in here so here is blank so that's the home page here register here login and here dashboard so for example we always go we always put in forward slash and first we can just say register and we can see it's rendering out the register page so forward slash register and the route name will register that is just how we depicted it there then we have my Dash login and dashboard so let's do my Dash login there we go log in here and let's look at dashboard and that's working fine so we've configured our views our URLs the basis of our Django application and we've also configured our templates as well Okay so we've got everything set up and ready to go so the next thing we can go ahead and do is we can go ahead and set up everything in terms of our views and our forums okay so let's get started with that right so what we want to do now is we want to go ahead and create a model form for our user registration or creation form so what we can do is we can open up our app here and we can right click and say new file and we're going to create a forms dot Pi file okay so we're going to essentially go ahead and create a model form now a model form essentially is something that we create can create if we want to convert our Django models to a form so like an online form that you've that you may have seen online so if you're filling out an online form where you enter in in your first name your last name or in any situation let's say for example you want to log into Facebook or you want to log into Twitter or Instagram there will be like a form where you need to put in your username password so that is a form essentially and what we want to do is create a model form which is where we convert our models so our Django models so let's say we have a Django model which consists of a let's say a person and a person is going to have a first name they're going to have a last name so they're going to have attributes such as first name last name going to have age and anything unique to that particular person so that's what we're doing here we're creating a model form so the first thing that you want to do is you want to import the user creation form which is given to you by default with Django so Django has a built-in model form that you can utilize so what we can do is we can import it by saying from django.comtrip.auth dot forms okay import user creation form so this is the default user creation form that Django provides you with in some cases you can use model forms to create your own forms as you desire okay but here what we're going to do is modify the existing form Django gives us to create a user next what you want to do is you want to import the default user model that comes with Django so essentially speaking if you were to go to settings.pi you can see under installed apps we have the default Aus app here from Django and this alls app has a model a default user model which we're going to use where we can add in a username an email a password Etc these are all Fields part of this app so to do that we can just say from Django Dot contrib Dot auth dot models import users so we want to use a default user model now what we want to do is we want to create our model form so we can just say class and you can give your class name anything that you want and I'm going to call it create user form and within parentheses I want us to inherit this user creation format given to us and I want us to modify it a little bit so this is essentially a base class that we're going to inherit so I'm just going to say user creation form and this is something that we just want to add into our parentheses okay and next we need to specify our meta data so I'm going to say class meta remember spacing is important here and now what we can do is say where do we want to specify the metadata from so it's going to be from the model which is user then we need to specify our field so I'm going to say fields and the default Django user model has a lot of fields okay I would recommend going ahead and reading more about that but the ones that we want to use is going to be simple it's just going to be username and you want to make sure this is in a list then we have email then we have password one and password to now I just want to make something clear here so we don't have two passwords okay but when you what you'll see when we are registering a user is going to ask us for a password that will be password one and password two essentially is going to be focused on re-entering password one so just as a verification check so in terms of fields we have a username field where a user will choose username we have an email field where a user will choose their email and then password one is the password we want to choose and that's that field and password 2 is just a field for verification in terms of verifying the password that's entered but not to worry this will make more sense as we get to it so let's say in terms of our form so you more than welcome to just double check so I will just move this up here so this is what we need to set everything up accordingly so we are going to use the default model user that Django provides with us and we want to use these fields in user registration next you want to go to your views.pi file and we can remove this HTTP response and right at the top here you can just simply say from dot um forms and forms.pi is in our CRM app here so you can see we have forms.pi so that's why we can just say the dot and we're going to say forms and we want to import that class which was create user form so remember this class name can be anything that you define so I'm going to say from dot forms import create user form because that's the name that we specified right here great so we've got that set next thing that we want to do is go to our register View and we can add in the functionality that we require so let's just add some space here okay and let's get started so here is our register review and let's get started so the first thing that we want to do is we want to go ahead and set a variable so I'm going to call this form and we're going to assign that to the create user form that we have here so this form is going to just be assigned so create user form like so next all we want to do is we want to make sure that we are sending a post request so this means we're going to be sending data to our database so I'm going to say if the request dot method equals that of a post request we can then add in our functionality now this will make more sense once we go to the register.html page I'm going to show you why we need to add in this functionality because we want to check if we are expecting a post request from this register.html page if we are okay we can just say the form now is going to be equal to create user form and what we want to do is based on all the attributes that we are using in the create user form so the username the email password 1 and password 2 based on all of those fields what we want to do is we want to post it to the database so we're going to say request.post next we want to say if form dot is underscore valid and all we're doing here is we want to check if all of the data that has been entered in our field username email password are valid and if we don't have any errors with our form right here so what you can do now is you can just say form dot save and essentially here form.save is the final method that you can add here so this officially pushes all that data if it's valid to your database and once you've done this you can just simply set return something so I'm going to redirect so that is a function we can add in the top here as a Django a shortcut so it's from the Django shortcuts package you can just say redirect okay and that's going to allow us to use that function so just under form save so everything if any every single success we can say return redirect and we can just redirect to a page that I'm going to go to the login page my Dash login so someone has registered for an account they're going to go to the login page and make sure that whatever you you specified right here correlates to your route name and of course what name you set here in place so I'm going to set it to my login and actually for safety I'm going to put in double quotes here so you can decide how you want to do that the next thing that we need to do is we need to render out our form to this register or that HTML files we need to render it out so I'm going to create a context dictionary here I'm going to say context equals and I'm going to set my key value pairs so the key can be anything that's what you reference in your HTML page so I'm going to call the key here register um let's say register form and the value okay is going to be form so as we can see we stipulated form here and that's going to be set as form okay so now what we can do is we can just say uh adding a comma and say context is equal to context and this is going to allow us to pass this form that we've created so this model form that we have here we want to pass it to register.html now here you can either say context or context equals context is just a developer preference you can decide what you want to do but essentially this is our view so I'll just zoom out so you can see so this is what we need to add into place for the functionality in order to register a user so all we need to do now is we need to just display the form here that's why we're adding it in a context dictionary and display it on the register.html page okay so we've got that set so now what we can do is we can go to our register the HTML page and let's just reference it accordingly so what you can do is just create a form and that form is going to take in the following method which is going to be post so this is why we set it to to post here so when we said if request equals mess dot method equals post we are just checking because we want to send it from the HTML here so this is why we're doing that check um and then I like to say autocomplete uh equals off I really don't like suggestions and that's fine and then what you want to do is you want to add in the CSR csrf um token this is a built-in tag because for every post request you've sent on a Jenga you need to pass a csrf token then here we can reference that form here so we called it register form as the key so here is where you ref reference it register form and I'm going to say dot as underscore P so this dot is underscore p is just going to add in some styling because it looks really ugly without it so I'd recommend you add it and then we can just put in a button here so I'm going to say input type is a submit button and the value is I'm going to call it register and that's all you need for that so this is what we need for our form so here essentially we are passing through the model form that we created which is going to consist of the fields that we stipulated and the fields that we stipulated if we can see is username email password one password two these are the fields that we are outputting now on our register that HTML file and of course that is based on the context dictionary that we passed on the form and as you can see there we assigned it accordingly to that variable and form is essentially based on the value and the key here is just how we reference it in the HTML so this is just like a like a placeholder you could say for the value that we're outputting okay so that's it so now what we can do is just restart our server and we should see everything on the page so we can refresh and let's go to the register page and there we go so here we go we have our registration page so we can add in a username we can add in an email address we can add in our password confirmation and register and when we registered it's going to redirect us as we can see to the login page so what we can do is actually give this a test so let's actually do that so enter a username I'm going to call this uh say James James mail.com okay and entering a password and register and there we go it takes us to the login page so if you want some extra validation to know that our user was actually registered you can always check it in the admin page so that's something that we can go ahead and check as well so what we can do is just create a super user so we can say python manage dot Pi create super user I'm going to leave a default as honor email blank internet password okay and run your server and now we can go and log into the admin page with those super user credentials to see if that user was actually created so James it was so you can just switch and add in the route name of admin and log in with your credentials all right and log in once you've entered in those credentials head on to users and here you can see okay we have our super user and here's James so it took in the username James it took in the email James at mail and we can see that set now you can see we also have the opportunity to use first name and last name as a field so when we set up our model forms here you can see the fields that we have here so what we could also have done is we could have also added in first name and last name as a field here um as well that's something else you can do but we were a bit selective here we wanted to keep it basic so we're just setting the fields that we want based on the model of user so this is the default user so you can see here um we have this model here that already exists by default was Django so the user model here now remembers Django automatically adds in an S so don't be swayed here so this is the user model and that is the user model in forms.pi that we're importing and as you can see here we're just inheriting that default user creation form here we're importing this here and in our class we're customizing that class and we're just altering That Base Class that we are inheriting from Django and just customizing the fields that we want to use for registration so I hope that makes sense Okay so we've got that set so now what we can do is just log out and that's it in terms of user registration so we can head on back to the page that we are at and now we can just go back to login there we go okay so now we want our users to log in and let's say they logged in we want them to then head on over once they've logged in to the dashboard so that is the process that we want to set up so now what we can do is we can just zoom out here so register is done we don't need to focus on this now and the views we can close for now okay so the next thing that we want to do is we want to go ahead and create another um model form but for authentication now with authentication Django also provides a default authentication form much like user creation there's a default one built in so we don't need to create a model class from scratch we can just customize it slightly like we did here with our user form so it's also good to just add in comment so here I just want to say register a user or create a user so we can say create or register and we can just say here um authenticate a user okay or what we can do is just say um we can just say at model form so we know that this is what it's for it's not the actual view functionality okay so let's start that process so what we can do is right at the top we want to essentially go ahead and just import the authentication form now you can just add in a comma here and it's going to be exactly the same so it's from djangocontrib.all stop forms and my I think uh we yeah I think it's the same one so Django contrib yeah it is so it's going to be from the same package essentially with Django all you need to do is right next to it say uh authentication form so right next to user creation form you can just put in a comma and say authentication form okay the next thing that we need to do is we need to go ahead and import forms so I'm going to say from Django import forms now we need to utilize the forms class here because we're going to make use of a widget so we're going to say from django.forms dot widgets import password input and text input so in order to use these widgets password input and text input we need to import forms and these widgets are essentially fields which are very unique and specific so text input would be appropriate for entering a username and password input of course for passwords so we can create our new form here so it's going to be very simple so we can just say class and I'm going to call it login form you can call it anything you want and then you need to inherit the base class which is going to be authentication form you can also copy this if you want and you just want to paste that in here so we can override it and now we can just set two Fields so username is going to be forms dot chartfield so it will be a Char field and we want to set that widget and that widget is going to be sent to text input and you want to open and close the parentheses then you want to set the password and the password is going to be sent to the same forms dot Char field and the widget is going to be set to password input and you want to open and close the parentheses so this is what you want to set up accordingly so your username and your password is going to be forums.trial field and you want to set the widget that you've imported so we want to use a text input and a password input and we just want to set that up here accordingly and make sure that and into place okay so that's what we need the two fields we need to username and password very simple great so we've got our form set now we can go to our views and set up Authentication so right at the top of the views.pi you want to of course in the first line here you just want to make sure that you import the login form so remember that is the name of the class name that you specified in forms.pi so that's what we set here so login form okay now what we want to do is we want to import the auth model so what you can do here is you can just simply say from django.com trip .os.models import all okay so we want to use the awesome model and then we want to make use of a few functions we won't use all of them but it's best that you know some of them so we can say from django.contrib.aus import authenticate so this is authenticate function that's something we will use a login function and a logout function so some of these we don't need to use some of them we do but just for your um notice so authentication models and function and functions so just helpful to know all right so we need to import that now we can go ahead and set up our business logic in our login View okay here's the login okay and you just want to move this up okay and it's some space okay so we're going to do the same thing as before so we can create a variable call form and we're going to associate it to login form next we want to check if the request Dot message is going to be that of a poster post request okay and then what we want to do is we want to set our form to login form again and we want to say all the data that is entered within the login form so we have two Fields username and password so all the data that is entered in there okay we want to ensure that that request gets sent as a post request so all the data gets sent as a post request okay then we want to check if the form is valid so if the form dot is underscore valid so if that form is valid what we can do is we can go ahead and we can just set the username and I'm going to say um the username is going to be request dot post dot get and we want to get the username so essentially we want to get the username here and we also want to get the password so next we can say password is equal to request.post dot get and we want to get the password so once we've obtained the username and the password we want to we want to check this in our database so we can just create a variable called user and we want to use the authenticate function now which is something we imported right here so we want to use authenticate function and that will Syndicate function here is going to take in a request and it wants to check if the username in our database which is called username is equals to the data that we entered in our field username so we're just checking if the password is equal to the password and the username is equal to the username so essentially we're going to check if the database username is matches the username entered in our form and if the password in the database matches the password we entered in our form and we're going to use authenticate function to check this if that user exists we can go ahead and do some functionality here so what I'm going to do just below is say if the user is not none so if the user exists essentially we want to use the alt function to allow that user to log in with a request and if that's the case we can redirect them and we can redirect them to the dashboard so all logged in users will that are all authenticated users should I rather say will be redirected to the dashboard so remember this is the auth.login um we're using the auth function here and we're going to utilize it with login which is based on what we are importing right here so these functions we're using these functions in Tangent okay and that's essentially what we need the next thing we need to do is create a context dictionary to Output these model um our model form so I'm going to create context dictionary so here the key will be login form this is what we reference in the HTML and the value is going to be form because remember we just referenced that as form here to this variable that we created and that's going to house all those widgets so the fields the username and the password field which we want to Output onto our mylogin.html page next we can pass that in so I'm going to say context equals context or you can just leave it as context choice is yours okay so now we can reference that in our login page so we can go to login and we we technically have some logic in register so we can literally copy this so in register we can just copy this um logic okay that's literally all you need you just need to now remove this key here because remember the key we said was login form so I'm just going to switch that to login form all right there we go so we also have the post request which we are checking for right here as well so just for clarity there and we can just test if that field if that actually works so what we can do is head on back refresh and here we have a username and a password field so we can just change the text there from register to login um did we get and technically speaking if we have an authenticated user we should be redirected to the dashboard so just make sure in your URLs that you specified dashboard here recording in the name parameter and the route name as well so let's test this out so we should see the login page if we put in a correct password so what I want us to do is I want us to first check a incorrect password so I'm just going to put in something randomly um let's put in say James and I'm going to put in the wrong password okay there we go please enter a correct username and password okay that's good and it's put in the real password and we get redirected to the dashboard page perfect so there we go we've added in user authentication for logging in all right so the next thing of course which is probably on your minds is now that we've done that how can we actually go ahead and ensure that we're able to log out so this is actually very simple so we can just create a um let's say we were to head on to our view all right we have our dashboard page here which is fine but just below we can just create a another view I'm going to call this Steph user log out pass in the request and you just want to say ALS dot logout and request and then you can return and redirect um we can redirect back to the home page if we're being logged out and that's all you need so you just need to use the alt function and you want to allow them to log out so you're going to log out of that session or that request and you're going to be redirected to the home page now what we need to do now is reference this in a URL so in our urls.pi file you can create another URL so we can just give it a pass it's going to be user Dash login as a route name The View was user log out underscore yes and the name user logout okay so you just need to set that URL as well and the reason you need to set this URL is because we need to add this option as you can see on the dashboard so if we're on the dashboard we want to be logged out so we want to be redirected so what's going to happen when you click that URL you're going to be redirected to the home page here because remember that's what we stated in terms of the route name so it's blank empty strings so now what you can do is you can head on over to your dashboard template and you can just add something in here so essentially you just want to add in a link to head on to the logout page I'm going to say a h or F and this is just going to be a link and we need to specify it as follows so you want to add in a curly brush percentage symbol and we're going to say URL and this is going to be user logout so user logout so that's what we stipulated here in URLs so the name and the route name so we just need to set that in and this is going to make this URL clickable so we'll head on straight to uh we're going to execute this view here so user logout we're going to execute this and it's going to redirect us back to the home page so we're just using this URL here as an entry point to execute the view to return to the home page and then what you want to do is you just want to close this href so we can just say log out here and just make sure your server is running so you can restart your server that might be best and we can just refresh and there we go we have an option to log out here so if we click on this according to our function will be logged out and sent to the home page here we go so that's it so we've now learned how to register users or create users how to perform authentication in terms of logging in and logging out now there's one more thing I want to mention and that is how you can go ahead and protect your view so what I mean by that is let's say we have the dashboard and we don't want and also unauthenticated excuse me users to view the dashboard page so you can see here I'm logging out here and I'm still able to head on over to the dashboard page as an authenticated user so what we can do is we can add in a login required decorator so you can go to your views.pi and we can just bring this dashboard view up a bit so at the top of your views.pi file okay we can close this URLs this dashboard and this form so in our views.pi right at the top we can just say from django.com trip dolls dot decorators import login underscore required so from django.comtrib they're all decorators import login required so we're going to use this login required decorator to protect our view so make sure you've imported that then we want to choose on Choose Or navigate to The View that you want protected so in our case it's going to be dashboard so right in at the top of this functional dashboard I'm going to say at login underscore required and we need to put in the login URL so we're going to set that login URL and for me it's my Dash login so that is the URL for my login and we can add that in make sure to refresh your server so it's very easy okay and now let's refresh let me go to the dashboard directly and it's going to force me to log in first so you can see here it's now appending the login function on the condition that will only be able to go to the dashboard if we have logged in as an authenticated user and you can see this is how we can go ahead and protect our views okay guys so that's it so that is everything in terms of the basics of handling use authentication user registration slash creation also a little bit on how we can protect our view now in a lot of the videos I feel that the source code is um not too hard to forget or to the type should I rather say but I can admit that there is quite a bit of source code and you can make a uh send a typo here and there in terms of a syntax error just a mistype so what I'm going to do is attach the source code as well I will be sure to provide this because I know it's quite a lot of code so I can understand so I will be sure to attach the link to the source code in the description below so just be sure to check that out if you want to just download the source code but essentially guys that is it and what we can also do is as a final test we can just do a quick demo here so we're on the we can just head on to the register page okay so let me register a user so I'm going to say um chain no we can't put the same uh let's say um Kate yeah uh Kate mail.com register Kate log out go to the dashboard and there we go so there we go have it there's a short demonstration there for you and of course if you were to head on to your super user you'll see that we have a new user called Kate in our database of course you can add in some links here as well so if you want to redirect um I can show you that as well um so just something extra so what you can do is on dashboard we have a link here so you can technically on your home page just um on index.html put that in for register and we can have one for the login as well if you'd like tell you just at least have some basic uh linking structures there okay and refresh and we can get to log in here and register here all right that looks ugly let me just put in a space double space should do it there we go register and login okay so that's also added in so you at least have some sense of direction you don't need to manually put in the route names all right so that's it guys and I hope this was helpful to you and like I said I will put the link to the source code in the description below so you can check that out all right so that's it guys and as always thank you for support and I'll see you next time see you next time [Music] thank you
Info
Channel: Cloud With Django
Views: 28,100
Rating: undefined out of 5
Keywords: Cloud With Django, cloud with django, cloudwithdjango, CloudWithDjango, cwd, CWD, Django, Handle user registration and authentication in Django, learn django, authentication in django, authenticate django, create user in django, register users in django, django project, the basics of django, django development, python django, python, learn python, user registration in django, authenticate user, sign up in django, login a user in django, django login, django logout, web security
Id: Z3qTXmT0yoI
Channel Id: undefined
Length: 58min 35sec (3515 seconds)
Published: Mon Jul 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.