How to Create a Login System in Python Using Django? | Python Projects | GeeksforGeeks

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone welcome to geeksforgeeks my name is anubhav madhav and in this video tutorial we are going to see that how can we create a login system using django which is a python web framework and that too in a very easy fast and simplest way in this web application user will be able to register on a website and then user will also receive a confirmation email in order to activate their account and then they can login to a system at any time as we are going to focus more on the back end therefore we will be making use of a very simple html based frontend you all can modify your front end according to your desire or needs anytime so first let's have a look at the demo on working of the login system which we are going to develop in this video so here we are at the home page of our web app so when the user will visit our website they'll click on the sign up button and the form for user registration will open let's say i'll create a username anu my first name last name i'll enter an email for registration i'll create a password let's say one two three one two three and click on sign and you can see that it says your account has been created successfully please check your email to confirm your email address in order to activate your account now i'll go and open my gmail of the email i entered here i can see that i have received an email first one is a welcome email for just a welcome message that hello nepal the username which the user will enter on the website welcome to gfc thank you for visiting website we have also sent you a confirmation email please change please confirm your email address i'll go and check for the other email sent by the app which is welcome to gfg django login hello and above the first name of the user please confirm your email by clicking on the following link and now i'll click on this link and here you can see that your account has been activated and now i'm redirected on a page to login which is sign in now i'll enter my username again which was a new enu and the password one two three now i'll click on login and here you can see that i'm successfully logged in which also displays my first name therefore it is a message that yes we are successfully logged and as we are logged in now our app shows us a button to sign up and when i click on sign out it just logged out successfully let's say i'll sign up once again with the same username and the same name well doesn't say the first and last name of any two persons can match but the email cannot so if i enter some different email this time but the same username let's see what happens and i click on sign up it will say that username already accessed please try some other username so i'll try some other username this time let's say anu one two three at the rate one two three which uses a spatial character let's say the name as a number mother and abc gmail.com one two three four one two three four click on sign up and it will say username must be alpha numeric so basically a web app checks for many such conditions now let's say i'll enter everything correctly this time i'll enter one two three first name is anubhav second name is another email now i'll create a password click on sign here you can see that we have received a message that your account has been created successfully please take your email to confirm your email address but this time i won't confirm this email address and now let's have a look at our admin panel so i'll type in here admin enter and enter my admin credentials which is also known as super user which is this for now and login here i would like to see that what all users are logged into my web app i'll click on users and here i can see that it is a super user named anub which is myself the admin here you can see that super user status is checked so that means this is the admin i'll check for this this email which is active which means the email which i confirmed for the first time and now it is active and this one which i haven't confirmed yet we can see that it isn't active yet also we can see the last time some user logged in so the date and the last time when the user logged in we can see all such things actually creating email confirmation feature in your website or your web app is a good practice it also saves your data and time for execution a lot as there can be some users who intentionally enter wrong email address and this may even crash your web app if you haven't handed exceptions correctly so it is better to have an email confirmation feature in our web app always and implementing that too is very very easy well this was a demo and this is the kind of web app which we are going to develop in this video so without any further ado let's go the prerequisites well for this you should just have basic knowledge of python and nothing else and the requirements that your device or laptop or desktop should have python installed in it and if you have not installed it you can install it from this url and don't forget to add python to the path in your environment variables if you don't know that how to do that you can refer to one of our blogs and the next steps which we are going to follow is first we will install the virtual environment it is optional but recommended it is just for our comfort and especially if we go to deploy our websites then it will help us a lot to make requirements file accordingly and then we'll create a xango project and we'll create a django app and then we'll start with the development here are some commands which we are going to follow in this video tutorial which is for installing creating and activating virtual environment creating a change of project and app and then creating a super user for admin okay so now let's start with the development what i'll first do is i create a folder in let's say gfg and i'll open my command prompt i'll cd to my desktop that is change directory to my desktop i then cd to this folder cd gfg and now what we're going to do is that first we will enjoy virtual environment and for that the command is tip install virtual env and then press enter but i have already installed virtual environment in my pc so it will just take it from the caches for you it may take some time okay now we have installed the virtual environment in our pc the next step is to create the virtual environment for that the command is virtual env and then the name of the virtual environment we can name it anything for now i'm naming it as p e n v and then press enter this will create a virtual environment for us so that whatever the library is which we are going to use in a project will be stored in this virtual environment and not exactly in a pc okay our virtual environment is created now in order to activate virtual environment in windows the command is ve nv backslash scripts flash activate and then press enter now you can see here that we have virtual environment activated and the commands for map and linux is source name of the virtual environment slash pin slash activity okay so first thing which we are going to do is we install django for that command is pip install triangle as you can see this is successfully installed django in my virtual environment not in my system you can use any text editor i am using visual studio code for this video tutorial and to open this the command is code dot dot means current directory so it will open a visual studio code in the current directory and then press enter as you can see that my visual studio code has been opened in the directory mentioned and we can also see the files of the virtual environment close this and now the next step is that we'll create a django project the command for creating a xango project is django admin start project and then the project name so we'll type in django admin start project and let's name the project as gfg okay press enter now we can see that our project is successfully created and now i'll open this folder and the good practice which is followed by many developers is to cut these two files in folders and paste it over here so that your project directory also lies in the root directory that's it now we will create a django app for that command is python manage dot py start tab and then the name of the app so we'll type in the command python manage dot p by start app and then the name of the app let's name it as authentication so this has now successfully created an app for me named authentication now the very first thing to do is to open the url.py file of django project and we create a path comma include app name dot urls and our app name is authentication dot urs then place a comma over here and we have not included include so we'll type in here include and as we can see that i do not have any urls file over here but we have mentioned it so we click on this click on new file urls dot py and then press enter but it is empty for now so we just go here we copy this code paste it in our urls or py we remove these things and here we will set urls for our app so the first url which i would like to set is for the home page so i'll type in path so now we will open our views.py and we'll create a function named as home as we have mentioned over here that the name of a function is home it means that when the user will request for this url which is the nothing after the domain of the website we'll call a function home and here we will type in a function that how will it work we'll pass a request to the function and then for now i'm just returning a http response which says hello i'm working as we have made use of this http response we also have to import it for that we will write in from xango.http import http response in our urls or py of the app we will also include our views for that we will type in from dot import views and then save it now we will try running a server for that we type in the command python menus dot py run server and now our server is running on the development server which is the url this we copy this we open our browser and we paste this url over here and then press enter and now we can see that it is returning a http response which says hello i'm working so that means our django app is working perfectly now before diving directly into the login system let me first tell you that how a django app works so first thing when you deploy a django website or even run it on a local post so tango first search for a file called settings.ppman and then it will check that what all settings are you using for your app so the app will run accordingly and then it checks for the urls of the project now you can see that for this url we have included the urls of the app named authentication so in the authentication app it will go in the urls it will again search for this url and according to the url it will run that function and our function which is written over here returns a http response which says hello i am working so that's how a django app works and if you're making use of some models or database over here then it again goes to models.py after that and then returns a response accordingly to your views and then your views may return either a http response or indirect html template okay so now for login system what we need is basically a home page which we already have next thing which i would like to have is a sign up page where user can sign up and register their details so i'll create a url for signup which is something like this i'll name the function as sign up and name this url as sign up to similarly i'll have it for sign in so i'll just press ctrl c and then press ctrl plus v over here then ctrl c and ctrl v again well now i'm going to tell you a shortcut which you might not be knowing or might be knowing about the visual studio code so if i want to change sign up to sign in i'll click here and then again i'll press left alt and then click here and i'll keep pressing left or and then i'll click here and now i'll press my backspace and instead of sign up i'll just put in sign in and see how easy that is let's see if you have to make same changes at 10 places so this can help you by saving your time similarly for this i'll click here press alt click here press alt and click here i'll write in sign out as simple as that now we'll write functions for these three for sign up let's say if sign up keep passing request and here for now i'll just return a html template for that i'll write in return render with asylum request comma name of the app which is authentication slash and the name of the html page which is signup.html we haven't created that html template yet but we'll do it soon similarly i just copy it and do the same thing for sign in and sign out sign in i'll write him sign in over here and sign out well we don't need to render any template for sign out so i just type in here pass simple now the next thing is to create html templates for the app for that what we will do is i'll just close all these so that it won't confuse you i'll create a folder over here named templates and then again inside that folder i'll create a folder named with our app which is authentication inside that authentication folder i'll create a new file let's say index.html and one more file which is signup.html again one more file which is signin.html and also in our function home we can render index.html instead of passing http responsive typefication index.html simple so now let's create templates for our app in order to save time i'll not focus more on the front end so i'll just make use of html5 boilerplate and here let's say we'll type in the title of the app as authentication and now i'll remove this let's say we display our heading which says welcome to our app for now let's say welcome to geeks for gigs and then i would like to have a button so when the user clicks on this button they'll be redirected to the sign up page so the type of the button will be submit and let's name it as sign similarly i'll do it for sign in and one more button for sign out okay now next thing which we are supposed to do is that we need to tell django that what is the directory of templates which we are going to use so what we will do is we open settings dot py we will search for templates and here in the directories we will type in templates simple and now you can see that the server is still running we'll go to a browser type in the url and press enter now you can see that html template for the index is rendered but when i click on these buttons nothing is happening so what are we supposed to do is that we need to give these buttons the urls of the functions written for them so for that we open index.html type space and we i'll type up here a which is a anchor tag i'll cut this here and paste it here and i'll pass the url for this as slash sign up this should match exactly i said in my urls so similarly for sign in i'll copy this from here paste it here again and instead of sign up sign in instead of sign up sign okay so let's see that what is a function for sign of doing it is doing nothing but rendering a sign up dot html template which we haven't made yet let's work on signup.html i'll again use html5 boilerplate and remove this and here i'll write the heading sign up let's see whether it is working or not i refresh it i'll click on sign up and it is working so now the next thing which we are going to do is it will create a form so that the user when fills the form will be able to register to our website so that i'll type here form inside that form i'll put a label name the username and then i'll take a input of type is equal to text for which id is equal to username and then name is equal to username and then i'll mention a placeholder which says create a username and in the app you're going to use only letters and numbers for the username only letters and numbers and then i'll make this field as required similarly i just copied these two things and then paste it for the first name the last name of the user email of the user the password of the user and then to confirm the password of the user so let's change it accordingly this is first name this is last name this is email address this is going to be the password this user will create for it himself and then this is going to be confirm your password again use that shortcut over here name for this i'll use a name for this let's say we use email a password let's say we can use password password for this we'll use pass to also don't forget to change the type of the email address as email the type of the passwords as passwords in the user type in password it won't display it will come either as asterix or dodge and now we will change the placeholders for them which is enter your first name and then for this we are going to say enter your last name for this email will say enter your email address for this we will say create a password and here we will say confirm your password and then we'll save it in django don't ever forget to use csrf token just after a form i'll type in csrf token and press enter it is the ginger tags which django templates use you don't need to go deep type that what csrf token is whenever you make use of forms in your django app remember to use csrf token now for a form we are going to have a button of type is go to submit you type in here button press enter just type is equal to submit i'll name the button as sign up and then we are supposed to mention that what kind of method is a form using so it will use post method action means that when we submit the form what action will this form take so for this action we'll just route this url to sign up let's also add pr tags over here just to add spaces and then this we will save it and then we go to our web app and click on sign so this is how a form is looking like for now without any css but if i fill this form and then submit it you can see that nothing is going to happen because it will just reflect on itself because we haven't created a view or a function yet that what are you supposed to do when our users fills the form and clicks on sign up so for that let's come back to our views.py and write some code here so as you might be remembering that the method which our forms uses is post method so we'll type in here if request dot method equals equals post then do this so the first thing which we are going to do is that we will take in all the fields entered by the user and store it in a variables so that we can play with them or work on them on a back-end so the first thing was username let's name it as username itself and how are we going to take it or request it from the user so we'll type in request dot post dot get and then the name of that input tag which was username as you can see that in our signup.html for username we made use username as any variable and for first name we used fname for last name we used lname so accordingly we are going to do this in the views dot py if you want you can write it like this and if you want you can write it like this too this dot post and then just this username anything will work well let's go with the second one for now so for the second thing first name you can request dot post f name i'll just copy this and paste it over here and replace f by l for the last name and then the next thing is going to be email so we request that to represent post the name for the email was used as email for password one we use password for confirm your password we use password similarly us one we can name it as password two because this is the variable which we are going to use in our back-end but for now let's say we are using password request or post password and similarly for pass to which is confirm your password okay so now we have taken all the input from the user in a back end now what we are supposed to do is that after the user submits all these inputs we will register that user in a backend that is our database so for that we will make use of a tango inbit library and we import it as by typing here from django dot contrib dot dot models import user save it and here we will say that we create a user object let's say we can name it as now as my user is equal to user which is the model which we have imported from the contrib.org models of django here we will say that user dot objects dot create which means create a user for me whose username is username password the user and then the email and then the password well these are the three important things which we are supposed to mention over here if you want we can also pass its first name by saying my user dot first name is equal to fname similarly we'll do it for last name my user.last name is equal to element and then we will save this user in a database for that we will say my user dot save now let's do one thing that when the user is registered successfully we shall show him a message that yes he was registered successfully for that we are going to make use of messages which is also an input library of janko that we'll type in here at the top from django dot contrib import messages and here just after saving the user will say messages dot success success and then we'll pass in our request then we type that message which we are supposed to show to the user which is your account has been successfully created create and now what we want is that as soon as the user registers himself he or she should be redirected to the login page for that we'll type in here return redirect and then the url for the sign in as you can see that there is a yellow line on the redirect so what i'll do is just go over over here and we is holding itself tell me the quick fix for this so i'll just click on quick fix over here and it will show me that from triangle to shortcut input redirect comma enter i click on this and it is automatically import it for me okay so let's see that how is the app working for now i'll create a user let's say anu and then then i click on sign up we can see that it will show an error because create takes one position argument but four were given that one thing which i just noticed is that instead of create we are supposed to mention over here create user so now let's see that our app is working so i'll open this click on sign up enter a username and then the first name last name email address anything for now password one two three continue your password and then click on sign up now you can see that it shows an error no such table as auth user so what we forgot to do is as he made use of this imported model so we were supposed to run make migrations and then migrate the changes for that i'll just press ctrl c on my command prompt and then i will type the command to run make migrations which is python manage dot py make migrations and then press enter and then i will say python manage dot py migrate so it has migrated all the changes and you can see that it has my also migrated changes of the admin and the auth inbuilt models now what i'll do is i again run my server press enter our server is running now and now i will open my browser click on sign up again fill the details anything for now password is one two three and then i'll confirm the password as one i click on sign up and then it redirected me to sign in and it displays nothing because we haven't created any template for sign in yet so what i'm going to do is just copy all these just copy all this code from signup.html and paste it and sign in.html and remove all the last four inputs and for this i'll just login using the username and then the password of the user so this is going to be something like this type visible password id is equal to pass one and the name is equal to password and here we'll say your password and here we will save enter your username then we'll change the name of the button use here the next thing which we are going to do is that we'll change the action of the form instead of signup now this will go into the sign-in view so we open views dot py and we'll see that what our sign in view is doing it is doing nothing for now so now let's write some code for sign in which is that the user will click on sign in it will log in the user for that now again we are going to do the same thing which is if request dot method is equal to post then done the following thing which is first we will take the username of the user as we are doing it this in the sign in template first we are taking the username and then the password so we are again going to take those two things from the user in this function and the way to do that is using invisibility request dot post and then type in the name used for that input which is username here and password for the password and then we type the same thing for the password let's name it as password request dot post and then the name last one next we are going to do is that we will authenticate this user for that we will create a user object or a variable let's say user is equal to authenticate and to use this function which is an inbuilt function we need to mention this over here which is from django.contrip.auth import authenticate so that the django will authenticate this user and check that whether the password entered by the user matches as that of in a database or not so we'll type in here the user name passed by the user and then password which is password given by the user okay and then we'll check that if user is not none this thing may either return a non-response or a not none response it will return a none response if the user is not authenticated and if the user has entered write credentials this will return a not none object so which means if the user has given the right credentials and is already in our database so we'll login that user for this we will type in login request comma user again we need to import this login function so i'll just hover over it click on quick fix which will import from django.contrip.org import login this one this looks something like this now from tango dot content dot import authenticate comma login okay so now as we have logged in the user but what if the user do not matches or the credentials are bad so for that we'll write else messages dot header request let's say add credentials and we'll redirect this user to the homepage for that we'll type in return redirect home and similar to this user who has successfully login we'll type in return render this html template for that type and return render request comma app name slash the template name which is index.html and along with that you will also pass in a dictionary which is popularly known as context which contains the first name of the user so that will make use of this first name in the front-end template which will display a message like hello first name of the user so for that we'll create a dictionary with the key named as fname and for that the value is fname and for that we also need to mention that what f name is this f name is equal to user dot first name as simple as that now we'll check that what happens in the app now i'll refresh this and enter the username of the user created by me for the last time i again enter the password and enter 123 and click on sign in and it do not displace the name of the user for now because we haven't mentioned it in the index.html file so for that what we will do is we'll open index.html and we'll mention if user dot is authenticated which is if user is logged in then display the following things which is hello and then the first name of the user which we pass in as a dictionary with a key value named as fname and we show a message which says you are successfully logged in and to display the success error messages we also need to mention over here a ginger tag percentage sign for message in messages we create a div over here with class alert and press enter and then the class of the alert will be alert dash message dot text and then alert this miscible fade show of which rule is equal to hello and then we'll display it as bold with message and then the actual message from the vacant which is going to be something like this and then a span with area hidden is going to true so that user can even close that message down here i will write ampersand times let's take it inside a button with type is equal to button not submit class is equal to close in order to close this and data dismissed is equal to allowed and the label of the area this area label is equal to close and then we'll end this button after this okay that's enough and what happens if the user is not authenticated so then we are supposed to show the sign up and sign in button option to the user and the user is authenticated we'll just show sign out button okay and as we have ended this if block we are also supposed to end the for block for that we will write end for and press enter and now let's see that how is our app looking like again enter the username and above m the password one two three and then click on sign in now you can see that it says your account has been successfully created welcome to geeksforgeeks hello and above you are successfully logged in and then there is a button to sign up but as i click on sign out nothing happens and it shows an error because nothing is returned in the sign out function for now so we'll open our view.py we'll go to the sign out function and remove this pass now what we will do is we just simply write in here log out request which will which will log out the current user which is requested for the same url and then we will say message this dot success request comma in the message which is logged out successfully and then we'll return the reader url to phone to import this logout i just go over here click on quick fix and then i'll import this log out now this looks like from django.contrib.org import authenticate comma login command logout and now let's see that how is a logout function working so i'll open the app again login the user click on sign in and then i'll click on sign out now you can see that it successfully displays the user is logged out successfully and then displays the sign up and the sign-in button if the user is already signed in then it will not display the you sign up and sign button but will display the sign out button so that's how it works okay so this far what we have done is that our user will register himself on a website will then login and can then log out from the system now what we are going to do is we will implement the confirmation email functionality for our app and in order to implement the email functionality the first very important thing which we need is an email account itself for now let's make use of a gmail service so what you're supposed to do now is create a gmail account for your app or if you want to use an existing one you can do that too i have already created a gmail account for my app and the next thing which we are going to do is just open a new tab and search for less secure apps click on the first link and then make sure that you are signed into the correct gmail account and then allow less secure apps toggle on this button and that's it the next thing which we are going to do is open a vs code in the project directory which is gfg we'll create a new file called info dot py which will contain the information about our email account first thing we'll write as email use tls whether our email will use which says which which says that whether our email will make use of tls or not the next thing is email host for now which is smtp.gmail.com it depends on the service which you are using the next is email post user that is the gmail account for now it is cfg.demo dot tango dot login gmail.com next thing is email host password you type in my password over here it is not password for now and then the code which we will use for sending emails which is 587 okay so now i'll stop this recording for one minute and i'll enter my password over here and you can enter your password according to your email so let's go so i've entered the password of my email in the info dot py file and the next thing which we are going to do is we'll open settings.pbi and we'll mention all the email settings over here for that come on the top and just below this line right from dot info which is a file which we have made just now import star star means that input each and everything from here and now we give the settings for emails which is email use tls equal to the same thing email use tls the variable using the info.pyby next is email host is equal to email host then we'll say email host user is equal to email host user next is email host password is equal to email host password email port is equal to email okay so now what we're going to do is that in abuse or py we'll write a function in such a way that it will send emails to the user but before doing that let's do some validations for the app which is like that if the username already exists in our database then the user will not be able to create another account with the same username so we'll write here if user dot objects dot filter your username is equal to username let's say messages dot error pass in a request comma username already exists or created access please try some other username okay the next validation which we can check is that if email already exists so similarly we will write if user dot objects dot filter email is equal to email and just after that we should not forget to return or redirect it to some other page which is let's say home page for now i will say messages error passing request and then the message which is email already registered and then again we redirected to the home page similarly the next thing which we can do is that if username is if the length of username is too big which is let's say the length of username cannot be greater than 10 or 15 characters for now let's say let's keep it to 10. so we'll write an if len of username is greater than 10 messages dot error percent request the messages username must be under 10k excuse also the next thing which we can do is that if the password and the second password which is confirm your password two note matches password one not equal to password two so we'll say messages.error pass in a request and message will be passwords didn't match passwords didn't match okay and some other validation which we can have is that the username should be alphanumeric which says which means that it should either contains letters or numbers or both but nothing other than latest numbers for that we can write if not username dot is num like if username is something other than alphanumeric then return this error message which takes in a request and the message will be username must be alphanumeric username must be numeric and then again we redirect it to the home page okay now let's write some code to send the emails first let's try sending a simple email that can be a welcome message and how and which which will contain just a simple text how can we do that let's say i'll write this code for welcome email i'll first write the subject of the email which is welcome to jpg tango login and then i write in the message which is hello and then i must try writing the username or the first name let's see my user which is the object which we used my user dot first name and then we can say like add to exclamation marks and then for the new line write selection [Music] then the next message can be welcome to tft estimation marks for the next slides lesson and the same thing we can write thank you for visiting our website website and in the next line i want a message that we have also sent you a confirmation email question email please confirm your email address please confirm your email address in order to activate your account okay and then we can say something like thank you with two spaces thanking you and you if you want you can write your name too let's say thank you father and then we will send this email for that what are the settings which we are going to use so the email account from which the email is to sent is from email is equal to settings dot email post user and we are also supposed to import settings for this so for that we will write from the name of the project which is gfg from gfc import settings simple then we come again and then we'll come back again here and the next thing is that to whom is the email to be sent so to list we can even send this to multiple users but for now we will send just to a single user advance so we will write my user for email that is the email entered by the user on registration and then we will send the email for that we write send mail the subject the message and then the from email account which is from email and the receiver which is two lists and then we will mention that fails silently equals to true which means even if the email fails to send then it will do not let a app crash okay but for the send email we need to import it i just hover over it i'll click on quick fix which will import send mail from django dot co dot mail import send me so it will look something like this from django dot co dot made input send me and now we will save it for now we will again try registering our user for that i will run the server which is python manage dot py run server now server is running i'll open the browser i'll refresh it and then click on sign up i'll enter the new username let's say anu one two three four first name is last name email now i'm entering the email which already exists so that i can show you that this is working let's say i will write here an email account and now as you can see that it is not displaying any messages over here because we haven't used those messages tags in the other templates other than index.html so i just go to index.html copy this part and paste it in sign up to html and also in signing.html just go to the body tab tab click here and press tab okay refresh it and it here it tells that your account has been successfully created let's also say that during this after registering an account it will also say we have sent you a confirmation email confirmation email please conf from your account confirm your email in order to activate your account okay so i'll go back refresh it the server is running i'll refresh it now and here one thing that which i noticed it that instead of sign up i'm supposed to write sign in on sign in page and go to sign up and type in here sign in okay refreshed and this might have already sent up email to me so let me open my email app and show you that email received by me now as you can see that here is the email received by me just around two minutes ago when we registered the user and i click on this and it displays a message mentioned over there which means it says hello and above welcome to gfc thank you for visiting our website we have also sent you confirmation email please confirm your email address in order to activate your account but we haven't sent any confirmation email yet with a unique link so in order to do that the very first important thing which we need to do is that we need to generate a unique link how can we do that for that we can make use of a library called sex what i'll do here is i'll close this over by pressing ctrl c and i'll install one more library name six for the command is fifth install six now as the new library is successfully installed i'll again run the server now i will create a new file in my app named as let's say tokens dot py which will generate kind of tokens or a unique string for us here in token should be y i will write from tango dot contribute dot dot tokens which is authorizing tokens generated import password reset token generator which is generally used to reset the passwords here we can make use of that in order to activate the account too now i'll write from 6 import text type and then i'll create a class named as token generator which will generate the tokens for us which is inherited from the class named password preset token generator inside that class and make a function which will make hash value it already exists in the password reset open generator class we pass itself the user and the timestamp inside the function i'll return text type user dot pk which is a primary key a unique code for the user just text type timestamp so this is basically a unique string which will be used in order to activate the account of the user now i will just call this class celebrate token is equal to open generator that's it the very next important thing is that i'll just open my view.py and here what we are doing for now is that our user is directly created and is active but for now we will mention that my user dot is active is equal to false so that the user account will not be activated we will active this account as soon as the user will click on the link so now what we are going to do is we will write the code to send the confirmation email to the user which goes like let me write your email address confirmation email first i will take the current site which is whether it is working on localhost or wherever it is deployed it will take the domain of that website so i'll right here get current site and then i'll pass in the request i'll import this get current site i hover over it quick fix and here it is which is from zango.contrip.sites.shortcut import.getcurrentsite the next thing which we're going to do is i'll write the email subject as we already make use of the subject i'll use some another variable for this right now this email subject and subject can be check confirm your email add gfg login the next thing is the message this message to let's say because we have already make use of the message variable so message 2 is equal to we render this to string render to string we'll make a new html template which will make a new html template that email template will be sent to the users each and every time they request for the confirmation email that is email confirmation.html and i'm not going to create this in the app directory i'll just directly create this in the templates not inside the authentication but in the templates i'll click here on the templates i'll click on new file and i'll write here email confirmation html before adding the html template let's complete the email function the next thing which we're going to pass is a dictionary which will contain the setting keys and values for the user the first thing which i'll show is the name of the user which which is going to the first name basically my user dot first name will display message like that hello first name and then the domain of the website which is nothing but current site dot domain then i'll take the user id which is url safe base64 encode that is pk okay then place a comma and import these things by hovering over it quick fix from django dot template loadout import the render to string and the next is hover over this from django.utels http import url safe base64 encode and then for four spites we'll write that from tango dot utils dot encoding import force bytes we will also import one more thing which is force text and we make use of that data and after this i'll pass in a token which is generated by our token generator for that i'll write generate token dot make token and then my user so which means turn it open for my user using its primary key and time stamp i'll place a comma over here well this is not necessary if you want you can and then i'll import generally token by writing from dot tokens encode generate token this is force bytes not force byte and now we are going to create the email object which is email is equal to email message email subject the subject of email message variable which is message to then the host from which the email is to be sent email post user then the receiver which is my user dot email and then here are actually two commas one is to be replaced okay and then close it and then we'll write if email fails and fails silently is equal to true then send the email okay well what is this email message as we don't know we'll just hover over it click on quick fix and then we'll import from zango.co.mail import emailmessage.commail and save the file also make sure one thing that in our token.ppy it is from xango.contrip.org so now as we have implemented the function to send the email address confirmation email so now let's switch to html template for the email confirmation so let's click on this and what we're going to write in here is a simple text so for that we'll make use of auto escape and i'll just press enter and i'll put it as off so it means that it will not escape the html tags automatically so we can just simply straight away write simple text like welcome to chango login and then we can say hello then the username or the first name of the user and the variable for that is name so i'll write here name and then we can say please confirm your email by clicking on the following link something like you can see the confirmation link is here which will be http colon slash slash and then the domain of the current website which is given by this function the current site domain so we'll write it here the domain and then again we will just pass it to the url in order to activate the account so i'll make a new url named activate the function name also activate and then i'll pass in the uid which is uid 64 is equal to uid which is nothing but this thing to get a unique token and then we pass a token this unique id will check whether the token is of the particular user or not and this is going to create the token for us and so now let's make this in a urls so i'll open my url pyby of the app and i'll make a new url named activate but here's something more special so in this thing i'm also going to include this uid which we pass over here like this uid be 64 and then again the token and now i'm going to create this function in order to activate the account of the user so i'll come back to my views.py i'll make a new function called activate and i pass in request along with that i'm also going to pass those two parameters which i have passed in here which is the uidp64 and the token so it is going to be something like this along with the request we're going to pass in uidp 64 and the token and then we are going to create a try and accept block so in the try we will say that uit is equal to force text so it is so force text is something like encoding the spatial tokens and checking that whether this token was given to that particular user or not so we'll say force text and then url save p60 encode no this time it's not encode this this is going to be decode and then you'll pass in the id we are also supposed to import this so i hover over this click on quick fix and from django.utils.http import this url safe basic c4d code and along with the encode as we did earlier okay so the next thing is i'll create the user object my user is equal to user dot objects dot get i'll fetch the single user of whose primary key is this particular uid okay and then in the except block i'm going to say that type error value error overflow error the user does not exist like if any of these error happens does not exist and if any of these errors happens then i'm going to say that my user is equal to none then i'll validate whether my user is not none and generate token dot check token we're going to check this of my user object and the token person through this function so we say if this is true we can say that my user is active is equal to true that is we activate that user as in during sign up we said that my user dot is active is equal to false that is we weren't activating that account just on signups so as soon as the user would click on the confirmation link and if all the credentials like the user id and the token is correct then we are going to activate that account so after that we can say my user dot chief as we have made the changes so we'll save it and then we log in the user so we'll say request comma and we'll pass in the user object which is my user and then we can return or redirect it to home page what we will do is we'll just return render our new html template which will show our error message something like activation field and for this i'm going to create a html template in the root directory of my templates not within the app so i'll say activation field.html and within that i am going to write simply auto escape of activation failed please try again or anything like that you can choose your message on your own the other most important thing which we have missed is that in our views or py if you're not supposed to close this bracket here as we are passing this dictionary along with the template so we'll close this bracket here okay so now as we have completed the coding of our web app now let's see that whether our app is working perfectly or not so my server is running here i'll open the web app click on sign up create a new username let's say 1433 first name last name the email address and then i'll create a password and then confirm it i'll click on sign up you can see that it says your account has been created successfully you have sent your confirmation email please confirm your email in order to activate your account now let's check email i'll refresh it i can see that i have received two emails which is one welcome to gfg and the other is confirm your email so which says welcome to django login hello and above please confirm your email by clicking on the following link click on the link and it says welcome to geeksforgeeks hello you are successfully logged in click on sign out and it signed out me successfully so that's it this is our web app and the and the last final thing which we are supposed to do is that we will create a super user so that we can access our admin panel for that we will make use of a command which is python manage dot py create super user type in python manage dot py create super user press enter then i'll give a username let's say you press enter i'll enter an email you can get an email for now and then i'll create a password 723 and again the password then i'll press enter then it says that your password is too short do you want to bypass it i'll press 5 or not i mean yes so i'll press y and enter my super user is created successfully and now i can see this by going to the web app typing in slash admin okay our server is not running so i run my server now again go to this url which again ask me for the credentials of the super user which is a and u and then the password and login i'll see the users so now i can see all the users which i have created in this video and the recent one too which is r1433 so now our app is running successfully and we can sign out all right friends so this is how we create a perfect login system using django and tattoo in a very easy quick and simplest way you can access the complete code of this project by going on the github repository link will be given in description if you have any doubts or are facing any errors please write down in the comment section we will try to answer all your queries as soon don't forget to like comment subscribe and share it with your friends so that's it for today stay tuned to geeksforgeeks for more such amazing videos thank you
Info
Channel: GeeksforGeeks
Views: 20,428
Rating: undefined out of 5
Keywords: GeeksforGeeks, python django login system, login system in django, user registration and login in django, login system python project, authentication in django python, python project ideas, how to make a login system in python, python projects with source code, django login and registration source code, email verification in python, django sign up and login, django web apps projects, python projects for beginners, email verification login system using python
Id: 1UvTNMH7zDo
Channel Id: undefined
Length: 79min 19sec (4759 seconds)
Published: Wed Jul 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.