Django multi tenant tutorial using subdomains - basic implementation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video i'm going to show you how to implement basic multi-tenants with django using sub-domains the tenants will share the same database but they will be separated by the sub-domains so let's open up editor and get started first thing i'm going to do is to create and activate a virtual environment through this as a virtual env and then the name which is just example and i can go into it and then i can activate it by saying source bin slash activate so now i can install django just for this environment and i can set this task to done and then i install django and create a new project to do this as i pip install django this will install the newest version of django and also some few dependencies that django has great so now i can start project by think django admin start project example which is just the name of the project as well and if i go into it see the example i can start a new app so i can just add this to done just so that i know that i've done everything correct so create a new app for tenants and add it to the installed apps then i say python managed by start app tenant and i have open up open up the code here to the left so if we're going to example an example settings.pine i can go down to install apps and say talent and save but since i'm already inside setting stood by i just want to add example.com and dot example.com to my allowed hosts because i have set up on my local computer that example.com just points to one two seven zero zero one and this is so i can use sub domains so i can now save this go back to introduce and set this to done next step is to create the models for the tenants so if you just find the tenant up here open up models.pi here i create a new model called tenant clause turnout this is to keep track of the tenants in the database models dot model i want the name which is just a models dot char field max length can be set to 255. okay copy this paste it and just replace the name with sub domain and save say for now say python managed pi make migrations and python manage pi migrate to update the database and as you can see here i created a new model called tenant and then below here i create one more model clause tanant aware model models dot model i don't want this to be added to the database this is just something i'm going to reuse for the other models now here i want just one reference to the tenant so 10 ounce equals models dot foreign key pausing tenant and on delete models.cascade and then the last model i want to create is the member model which is members for the team or tenant so plus remember this is going to inherit this so i can copy tenant aware model and i can copy the name up here which would be the name of the member so if i now save i can update the database again migrate perfect so now i can set this to done and then i need to add these three models or two of them to the admin interface so if i find admin.pipe here i can say from dot models import tenant and member and then i register them by saying admin dot site dot register sorry register tenant and admin.site register member and then i want to create a super user so i can go into the admin interface and add a few of these so python managed by create super user admin admin example.com the password to command but i still want to use it since this is just a test then i can run the server again and if i now go to example.com colon8000 and slash admin i can sign in here with the user i just created and as you can see here now i can add members and i can add tenants so let's just add a two of them 10.1 10.1 save and add another 10 and 2 10 and 2 and save so now i have two tenants in the database just make sure that everything is okay i'm going to add one member to each of the team so member in tennant one and member in tenant two save so now everything there should be okay so now i can go back to the to-do list again because of this to done and i can set this task to done next step is to create a utilities file here i want to function for getting the host name and one function for getting the tenants from the database this is just to make things simpler so in here create a new file util it is and here i need to import the tenant model from dot models import tenant tenant and dev get host name pass in the request parameter return request dot get host dot split we need to split it on the port in case there are and then we just get the first one dot lower so this would give both the domain name or the the url you are on and then one function called get def get tanant pass in the request again hostname equals get host name so then we use this function just pass in the request parameter and then we need to get the sub domain sub domain equals hostname dot split just put it on the dot and we also want to get the first one because that is the one we have before our main domain name and then we say return tenant dot objects dot filter sub domain equals subdomain and we want to first in this list and save so now we're ready to use these functions in the view so then i can go back again set done next i want to create a simple view so we can see a team or a tenant and get its member if i just go to views.pipe from dot models import member and from dot utilities import get tenant def our team request and then we get the tenant business equals get tenant just passing the request now we get a tenant object in return numbers equals member dot object filter tenant equals 10 ounce if i go back to models to pi you will see that the member model doesn't have a tenant field but that is because we are using this we are inheriting the tenant from up here so then the last thing here is to return a render request and then the template name tanant slash our team.html now we can pass in the tenant and press in the members and save so then i can set this as well to done and then i can create a template for showing the theme in here create a new folder templates and in there one more folder tenant and in there the template our theme.html before i continue i just want to say thanks to my patreons here i just want to title claude here just want a simple title tenant dot name and h2 subtitle our members ul and here i can loop through the members for member in members ally member dot name then we need to close the and for loop and save so i can set this as well to done and then i need to import this to the urls.file so if i just find it there i can say from tenant dot views import our theme part our team slash pass in the name of the view and just set the name and save so now i have this to done the server is running but i want to show you my hosts file because to test this locally i set up example.com 10.1.example.com anthem2.exam.com to point at 127.001 so if i go to a browser to test this you'll see that i get this but if i go to dot tenant1.example.com slash our team ut member intended one and the title ten and one if i change this to ten and two with the ten and two and member in ten and two so this can obviously be expanded to use authentication and much more things like that but i hope you liked this video and if you did please click like and share below if you have any questions feel free to leave a comment and answer as soon as i can so see you in the next video
Info
Channel: Code With Stein
Views: 7,549
Rating: undefined out of 5
Keywords: code with stein, django, multi tenants, django multi tenants, subdomain, learn django
Id: _wefsc8X5VQ
Channel Id: undefined
Length: 11min 43sec (703 seconds)
Published: Mon Jan 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.