django Multiple Users Registration | django tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys today I'm gonna show you how to have multiple user registration in Django to show that I have created a project named Jango register and I have also created an account SAP that will contain all our user related information also on our models and also we will do our user authorization and authentication in here so this project will contain two types of users one is employees and another is customers so supposing you have a website for your company where both your customers and employees can register and sign in or login however you definitely don't want both the types of users to have access to everything in your project so for example the employees will have access to the admin area but the customers wouldn't so I will print the two registration forms one for employee registration and one for the customer registration and both will have different permissions I have also created a templates folder which will have all our HTML files so I have created one registration page where we will ask the user whether he or she wants to sign in as a customer or an employee and then based on that we will ask them the user to the required to the desired registration page customer registration or implied education I have also created an HTML paper sorry index page which is basically our home page Ljuba will make it according to your requirements but your own things will look like just for simplicity I have just written here an h1 tag which is welcome so first I'm going to create my models user models in model on time hour week and I'm gonna I'm gonna have to fill models which reckon at the user model customer model my model you can think of models as your database tables so in my that means there will be one to do for user one table for a customer and another tables are employed so learn to win to create custom user model one is abstract is usual and another is abstract user so as you can see here there are movies to extend the default jungle user model 1 it's abstract base user and another is abstract user so a trapeze user is used when you want more flexibility and the Django default user doesn't satisfy them and the abstract user is basically just extending the default user model and adding some extra fields with a user model so today I'm gonna use abstract user so the user model will just extend the abstract user model abstract user model already have some default fields like first name last name then ease stuff and things like that so that's great on my first so what I just did here is I created two boolean variables one is if customer and another one is employee so what this is going to do this it's just gonna help us to identify whether the user is a customer or an employee another thing which is gonna be similar for both the user is first name last name if you want of course you can just omit this and not have it okay so as you can see we have extended our our default user model with the help of abstract user and now we will create our regular models and you can have anything that you want so there's for simplicity I'm gonna add maybe two models sorry I'm gonna have ADD and another one that's just a location okay which is very important in here is having a one-to-one relationship with the user here customer and the employee models will have a one-to-one relationship with the user model so what we're doing is we are just extending this user model and hat and it's gonna have a one-to-one relationship with this what does that mean one-to-one relationship is basically a foreign key the user will be a foreign key with you need being true so what we just did here is we created our user film which will have a one-to-one relationship with the user model and on D equals the model fat cupcake means if this user is deleted if a particular user is deleted which has a one-to-one relationship with the customer then the customer model I mean that particular customer I will also get deleted and primarykey equals to true means that the user in the customer table in our database the user will bethe primary key so this line basically means the relationship is basically life that a user is a customer just for simplicity you can can get like that so similarly I do not do the same thing in our with that employee model here let's say instead of location we want their designation I'm just making this up you will make according to your requirements so we have created our three models so what will happen for a recap we will have three database tables in our database one user on customer one employee customer and employee table will have one row for a user which is like extending the user class from before making migration we will go to our main project settings and here have actual original we are gonna write or to use your model equals in our app name where we have to use your model where we extended our genes our model we need to do this because we are using a custom user model not the default ones so before making migration don't forget to write no you can see I've made my my question okay so our my question is time so now I'm gonna use Django forms to receive and process submitted ones and data from the user there are lot of ways of creating forms in jungle today I'm gonna use user creation form which is a built in jungle one with that first I need to create a warm stockpile order in accounts okay [Laughter] so this user creation form inherits from module form class of Chango it has three fields which is username password 1 and password for a confirmation we do user creation forms one for the employee registration form and one for the customer registration form so remember as I've already seen the user creation form already provides username password 1 and password to field so whatever we write in here will be just extra fields that you want your user to fill up in your registration form so this is a home I wanna have Merlin I'm gonna create the and London is I want your designation before we move on I want this information means as soon as the form is submitted successfully I want I want to date down to go to my database in a single transaction so that no data errors offer that you doing bad I'm gonna use from transaction atomic we are going to do this in each of them for each of the registration form what this transaction dot atomic will do and if the block of code is successfully run then the changes are saved in the database and if there is an exception then the changes will be rolled back I'm gonna create nothing in here [Laughter] so all we are doing here here we are getting the instance one so we are shaving our data in our database but I did a mistake here with one big customer that first you remember in our model start by me for the user we have first name and last name so that fine that's why here we will have I have imported from this module class I have imported customer employee and user so in our transaction dot atomic we have created a method where we are saving the instance of a form which form our user creation one perform will which have username password then other things that we want so here we are saving the first name last name since it will have in our registration form but what's gonna happen is in our database in the user table suppose I am mom I just registered in the phone suppose with the name Jerry and Jerry Joe then my first name is Jerry and my last name is Joe so now so in our user table in the first name column it will be daring and in the last moon individual so then I'm gonna save it can you see that I wrote commit and when I typed come intercoastal false because before saving it to our database I want to save this to information so it's commit goes to true again um it wouldn't have stayed fresh name last name and then I just saved it I saved it in my user table after that I called this customer model and created a variable named customer then I created an instance user instance and I saved phone number that we got from our form and saved it how do the same with the employ sign up just instead of customer I'm gonna write enjoy in here [Laughter] mmm you know I also want to have a designation how did a mistake in here this will be mastermind dot right because we are even the phone number in our customer table but it has been a customer so for those of you who don't know what this clean data that can does is when a user submits the data via the jungle form Django first validates it and cleans the data so this is because any data user submits to form will be asked to server as strings no matter what type of form field appears whether it's um our field whether it's char field or whatever it's gonna pass the data as a to server as string so when django cleans the data through clean data it will automatically convert the data to the appropriate type now i will create the views in visual pie folder means we start by file or in the account folder I don't have a page where the user will be asked whether they want to register as a customer or an employee for that I will create a method in view named register and it will just direct us to the HTML page in which is register dot HTML independent folder now you should really know our actual pitch we also have to create a URL this you know I mean magic you want to stop dying we also have to make sure that you are right so I have created this view method register which will just render us to the register dot HTML page here and I've also created the URL so if you can if you don't know how URLs work basically I have um pretty found in the main URL I have written tech towns so basically if you wanna access anything inside the accounts app in your website URL in the browser URL you will just write account slash then whatever URL you have written here so account / register slash we're gonna call register account slash register slash it will take us to this register method and this then this register method will take us to the template stock register dot HTML I hope that's clear register dot HTML I've just written my basic HTML if you're not acquainted with it that's alright you can just have whatever you want I'm just showing you the metal and just an example so here um in our register page I'm just gonna ask whether um I just waited a link whether the user one register as a customer or an employee let me show you in my local host [Laughter] it's really smart I haven't really added any CSS so it's not gonna take us anywhere no yes I didn't really add the link 14 customer and flight registration time now I'm gonna make two class-based views from your car check in to help upgrade that I have to avoid create view so the create view is a view that displays of one for creating an object and then saves the object if there is no object greater than it will return and I'm on the view are to display the data for the user model one glass will be the name of course one let's go tell the customers basically we just created our view for the customer registration form in here so it will this template name is the name of the template we wanna load and register the template that we wanna show when this custom register class page field is called so similarly just like register we also need to create a URL for this custom register [Music] as I did some mistake in here that is in our farm stock I don't forget to employ from Django import forms I've added finish show an error in our views do don't forget to import the this one file in here from the duck phone that means from the inside we are importing the 100 time since I named it one that I am can import form if you name it another thing a building it you don't import that no one from there we are importing our customer employ sign up which I have existing then rendering create view imported from generic view which is which are reduced in here and I have in court acting models from our age you go I mean I've called the user model in here so I imported in I imported this to us about what happen to me really need it so first let's go to our customers our customers gesture so I've just used to be sick um HTML template in here initially I wanted to use bootstrap but then I wanted to keep it as simple as possible so that's why you can see all these rules are no but you don't really need it you can just um design it however you want it to be so we will create this one film of one template we haven't really done it yet let me first check whether all this gives me any errors or not okay as you can see you can already see the file not the file I name the form so the reason you can see that is because we have used this the way you call form in HTML by giving two curly brackets so here the action is basically after submitting the form where you want your data to go to I'm gonna define that I actually want to go to our view which is um customer register what this is gonna do is it's just gonna call the URL my register if you have named this like mundi customer then you will call here only customer and then after if I'm here it's gonna put it up view which is in here and show us the render the forms another thing that I want to do is when I go to my sister and I when I click on it I actually wanted to go to that registration one page would you do that don't you do that I will go to my registration I'll do the same thing just protecting you so how do the same thing with the employee register J's I'm just gonna once after after practice let's go to that and apply registration of anything we're gonna do the same thing with it providing you our own Frankie register Thanks hmm definition okay again so now when I go to when we click I'm an employee when you come in implying it will sure they find now last thing we want to create is our admin drop by here basically whatever you want to see in your admin dashboard oh yeah you are not I mean we want to see the user i'm flyin customer ID does in here would you do that so normally you actually want to do a lot more than this um for simplicity I've just kept it like this let's see well you can see we have customer or if you want you cannot from here as well in employment panel or model we have this user as our primary key remember we created two boolean variables in models on pipe to identify whether the user is a customer or an employee in our forms to file when we are saving the data for the customer sign-off form we will make this um these customer now let's check whether our just wishing our everything is all I don't know why not it's giving of this ever because we haven't really really read them um after the submission of the registration form in your project you would want to redirect it to your home page or some other page for a simplicity I didn't do it here but I'm gonna show it to you how you can do it but first let's check in our admin whether the user has been registered or not in our customer as you can see the user is Brighton which we have taken from the user model in here or not Roberta anyways so it is customer active by default now the stuff first name last name here the location is blank because we didn't really include it in our form however if you want to you can as I said before I want my employees to do stuff never write that what I'm gonna do is I'm gonna say user but it's gonna do it's gonna give the employees the users who sign up as employees the permission of it stuff similarly if you want to redirect the user after the registration has been completed in our new start by what you can do is you can create a method here that a patient may be we are just making sure that the fireman what we did here is if the farm is safe then the user will be logged in and and you can redirect it to your desired page this is for doing direction all right so yeah guys this is it and next comes thumb logging logging bottom you can also do that with beam authentication for Django maintain authentication form which makes it a lot easier but I'm gonna sign off for today thank you for watching
Info
Channel: Bug Lover
Views: 17,048
Rating: undefined out of 5
Keywords: django, python, user registration, multiple user registration, django tutorial, custom user registration in django, custom user model django, multiple user registration in django, user registration django, django authorization, django authorisation, user model django, django authentication
Id: QnviZrFr6co
Channel Id: undefined
Length: 47min 19sec (2839 seconds)
Published: Sun Apr 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.