How to use views and ULS in Django Tenants

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm tom and welcome to this second video on how to use django tenants this is a follow-on video from the video i did where i set the initial app up i suggest you watch that before continuing with this video so in this video i will be setting up some views for the tenanted side of the app and for the public side of the app so let's begin so again for this example i will be using pycharm as it's my preferred ide so i'm just going to go over to pycharm first of all i have opened the existing demo app that i had in my last tutorial and i'm going to put i forgot last time to put the admin for the client in which would be quite useful so what i'm gonna do is i'm just gonna put in from from django dot control import admin i'm gonna go from client dot models import client i'm gonna go at admin admin dot register client and i'm gonna now put a simple class in called client admin and i'm gonna go admin dot add modern admin i'm now gonna go list display and i'm gonna put i want to display in the admin name and schema name oh let's just tidy the file up and let's run it so let's go and have a look let's call up the right page and there it is let's refresh and i'm on the tenanted side so you will see that clients exist if i try and edit by the way the other client so i'm at the moment i'm on demo if i try and edit demo two it won't actually let me you you watch this yeah it says you can't because you're outside your own schema that's a known thing so you shouldn't you shouldn't really let people edit this because it gives you an error but if i edit the other one demo because i'm on demo i'm gonna not change the schema name that's not a very good idea you shouldn't really want to change the schema name you can crash it right so that has put the admin in it's just useful to see what tenants we got really i'll get rid of that and put that back to what i have to right so going on to what i was going to talk about in this video which is setting up the first views so as mentioned in the last video it's really simple to do views so we're going to first of all do the one in the tenanted up so that sweet tenant i'm going to create a simple view which is going to basically do a list view from sweet so in here we're going to go glut class um and we're going to call it sweet we'll just call it index really because we're just going to doesn't really matter what we index yep and we do a list view import that and we want the standard zinc and we're gonna go model equals what do we call it we call it sweet and we're gonna do template name equals we'll call it index might be worth you putting all your templates under tenant or or something so you know that they're tenanted i actually put i if i'm doing it i actually would call it sweet i'll put it under the app name tenant slash index.html let's just create 10 there we are yes i would like to i'm going to create a really simple base file you don't have to but it's i just always do new html5 document and i'm going to call it tenant base all right block content i'm sure you all know how to use this and block now we're going to do block title and end block and block and now we're gonna just go copy those to our other one and we're gonna do extends tenant base and we're going to do tenant index let's just put it really simple tenant index tenant index we could put the name of the tenant that's always in the request object request dot tenant dot name it's quite a useful thing to do and let's put let's just put that in the title as well and block all right let's put the title ugh and i'm just gonna put that in there right now i'm going to go back to my view here get rid of that we will loop over sweets in a minute i'll just get i'll just get the page working first let's go back to the urls file now there's two urls file here the urls file we want in this case it's just called urls because that they're the tenanted ones so i'm just gonna put this in okay so what i'm gonna do here is i'm actually going to put my own urls file in the sweet tenant folder so i'm just going to move that out go copy paste it in there and go urls no it's not quite what we want but let's just call that file so i'm going to go in here i'm going to go path i'm going to say anything that starts with sweet actually we could do anything to be honest it doesn't anything and we're going to go and do an include include let's import that and we quit sweet tenant and we want namespace so let's put name equals sweet tenant keeps that going right now let's go to the you this urls file so i just jumped across to the other urls file let's remove all this junk at the top oh i don't want that and let's go here and let's go and put in the relevant code here let's put in on here let's put path equals and we'll just just put nothing and no regular expression don't need anything and let's put comma let's go and get the url the index index dot as view and put name equals index right so let's go and view the site so let's call it backup so we want demo dot demo dot um local host eight zero zero nine there we are you got that url so that was easy again we can do a quick query let's do a quick query over the uh suites so we go in there i like personally i like giving him different context names so let's create a quick table inside here table okay so that's basically done it here we are let's come back and there you are we've got that and if you notice so that's on demo dot local host if we go to the other tenant which is demo two we've only got a name of pink and candy so so what i'm going to do next is create a url for the main site so if i go to www dot local host you will see that only get that at the moment so let's quit create a quick quick template view for that right okay so these are url public what we're going to do is we're going to put these um views in the in here so quickly put a thing in here so we could do almost the same again copy this call this urls actually what i'm going to do to save a little bit of time i'm just going to copy the one from sweet tenant and paste it in there i'm gonna instead of instead of um creating an actual view all i'm gonna do is call it a template called a direct template view and i'm going to go i'm going to do an inline one basically template name equals client slash index dot html create template all right let's just pinch let's just pinch what's in base and just quickly put something in on the page don't want to take long doing this because it's almost the same as the other one so main public site main public site you can stylize this how you want right okay so one could do now is go into the urls public just basically go over from here and go i don't want any of that i just want to include there include client remember to import that client.urls comma name equals index right so if we now go to the main site main public site so that's really it for this video i hope you've enjoyed this video please remember to subscribe to my channel if you haven't already done so and that and please remember to give me a thumbs up thank you very much bye bye
Info
Channel: Tom's Python and Django
Views: 349
Rating: undefined out of 5
Keywords: django, django-tenants
Id: IrAz-q5rv3A
Channel Id: undefined
Length: 14min 37sec (877 seconds)
Published: Thu Sep 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.