Admin and Apps - Django Web Development with Python p.3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody welcome to a part 3 of the jingo web development series I'm your host centex and let's get into it today what we're gonna be talking about is the admin page of Jango as well as a an opportunity for me to show off just how easily we could incorporate other people's apps for all kinds of things and we'll just show a really quick example by the end of this one so let's get into it so the first thing we're gonna do is to use admin we need a super user we don't have one so the first thing that we're gonna do is create one so I'm gonna open up command prompt here and I'm gonna say Python manage PI create super user and then here what we're gonna do is pick a user name the default would be like your computer's name I'm using a paper space virtual machine here says why is this paper space but I'm gonna call mine syntax email address you can put one or not it doesn't really matter if you do have one later on down the line like right now we're running Jango with debug equals true so we get and see the debugging messages so it's no big deal later when you push your website to production Jango has a sort of reporting functionality because you won't be running with debug equals true and you can incorporate and allow Jango to actually email you when certain errors are hit so there could be a reason you want to add an email address here but just like everything else with Jango if you decide not to do it now like that you can always come back and revisit this later you can just edit the username and add the email address no big deal password I always use programming as my password Dilla or python is amazing 1 2 3 ok so we've got a user created and we're ready to rhumble side note of course don't make your passwords super simple for your admin account otherwise someone could log into your admin and then what I'm about to show you with admin should tell you why you wouldn't like that to occur ok so what we want to do now that we've got this admin user we actually could run this server and check things out so actually I'm gonna go Python managed PI run server because I don't have another one running right now and let's go ahead and head to our website slash admin boom we got a login and it's way zoomed in okay sent Dex and programming is awesome one two three okay and what we get here is the Django admin page here we can see a couple of things we've got groups and users we could go ahead and click on users boom there's your Centex user that you just created we could click on that user boom we've got the username password which is this hashed out password basically but then here we've got some personal information that we could change the reason why this is all here is because the admin user or the super user is just a version of a user and the user the Django user model has fields for name and email address and all that so anyways that's why that's already there now I know you must be wondering well what about our model because we just made a model in the last tutorial where's that at well you have to register your models so you go to the Django registration you cut out a little piece of the cardboard that your Django package came in to correct myself oh okay so what we got to do is go into Maine and we just go into the admin here and in fact I told myself I was gonna keep using this one so we go here and register your models here so all we have to do is actually import the tutorial models so from models import tutorial don't forget the relative period there and then all we need to do is admin dot site dot regice regice and we could do tutorial like that tutorial hopefully I spelled that right let's find out let's refresh boom tutorials interestingly enough this has an S appended to it even though the you know tutorial is singular I forget what it is there there is a method you can override eventually we will probably hit it but it because it's kind of annoying so a turn like series what it's going to do this just adds an S to it it doesn't have any handling for if the thing ends and s maybe do like es or or just a a an apostrophe ER so it doesn't it's really stupid it just adds an S so so anyway later what we can fix that but that's where the S is coming from it's literally taking the model name plus s so yeah but later on down the line when you're like trying to go back and figure out where is it getting the name from that's what's going on so anyway been there done that ok so that's how we could register it super simple and if we click on tutorials boom we can already see the tutorial that we actually made and all the fields are here and notice how like you the date published has this thing like today and now and you could click on a calendar and pick all these things are done for us and like for example here this is car filled and it's just like a short field and here you've got this big field here because this is a text field and I just think that's so cool if anybody's ever had to make an admin like control panel and ACP and needed to do all of this stuff that's all our work and even though this is like super basic I mean a lot of stuff went into this and that's pretty awesome so ok so we've got that done now the next thing I want to talk about is how we can kind of customize this slightly later on it's it could get even more crazy than what we're about to do but just to show you that you can do this what we'll do is rather than just registering the model so for example your models could have like 100 attributes but you might only want to edit a handful of them like some of them might be defaults like what was the temperature or something like that that you would never edit ok so you might not want all the fields you also might want to like organize the fields in different ways or in different orders and so on so or set for example a default so right now if I go to this tutorial and either add tutorial here or even back here you could say add so we could add another tutorial here let's just add a quick print tutorial tutorial actually I'm gonna save this for now I don't want to add add one right now I'll save that for later we will add some more but I'm gonna wait until after the amazing app changes so the first thing that we should do is how can we customize like the order and like the setup of tutorial here so what we're gonna do is we could say here class and we're just gonna say tutorial admin and this it will inherit from admin dot model admin so it's just it basically when we register all we're gonna do is actually register tutorial with tutorial admin here and this was just assumed before now we're just overriding it basically and these are the parameters by which we are going to override it we're gonna say hey we want the following fields and you'll just pass a list here so for example each field would be an attribute so tutorial title was one of them and let's just add the thickest it was three then we had tutorial content and then we had tutorial published should be right let's refresh for real quick make sure it's all looking hunky-dory show or enough okay but tutorial publish maybe we want that like up above right so what we could do is come into here and just change the order cut paste that was ugly oh so so ugly who almost got got by that not today Python not today okay so now we got the different order here another thing that we could do is you can separate them by fields just again if you had like a hundred you might want to like cluster them together or something so this we don't really have enough to show that truly but anyway I'll just show it real basically okay field sets okay so now we can actually separate them into sets so for example we could say title date in this set is fields and then in here we'll put a tutorial title and tutorial published then we'll have another set which will be called content and then in here we can pass fields and then tutorial content and I can get rid of that hopefully I'll fluid an error let's see what happens boom awesome Wow usually I would make a typo by then so as you can see we got these like little dividers basically for the date data types okay pretty cool pretty cool the next thing is that we could do is for example if I go to I don't want to delete let's go to tutorials add a tutorial you can see here like the date is empty one of the things that I wouldn't mind doing really quickly is changing our model here let's go models pi and here you see where we have tutorial published equals model that date time field then we call it date published one of the parameters that we can put into one of these is a default so I would like to default to be a date time now so what we can say is from date time import date time and then date time dot now boom save that and that would be a change to our model so I don't think we're gonna get away with let's see what happens I'm trying to decide if that would require I don't imagine that's gonna require a migration I don't know that's a good question actually let's see what happens nope didn't require a migration nice so now when you hit new tutorial boom that's already in there so why that didn't require migration I guess you would have to it must be Django is running like Python to set those defaults would be my guess because this is never it's not an SQL default right it's because this book as we when we hit new right here this doesn't create any SQL yet nothing is you know inserted SQL wise until we probably hit save so that's probably why we didn't need it migrate but we also could have found out like if you're if you're uncertain you're it's never gonna be a big deal if you if you get lost how many things are going to minimize if you just quickly do a Python managed up I make migrations if you just check well I would actually alter so it does want to alter it fascinating but it actually made the change interesting already forgot uh let's just I guess we could go ahead and Python mashup I migrate fine oh okay hey that's interesting even though that was already hint it didn't seem like a migrate it was required anyway okay very very interesting so the big thing now is when I come into here like imagine actually trying to write tutorials in here now maybe not everybody here is written a tutorial but this is like plain text which would be really really painful in very tedious also you guys probably don't want to read Python code written strict straight into there that would probably not be pleasant so instead what we want is some sort of like editor and they make editors called like what you see is what you get types of editors now I just did a quick search for some editors for Jengo and I found that there's one called tiny MCE for - light so all we need to do to grab that and that's like it's a jangle app that we can just add to our app so all we need to do first is install it so I'm gonna come over I'm guessing this is nope okay so I'm gonna come over here and real quick I already forget if pip corresponds to three seven or not yes so I'm gonna go pip install Django - tiny MCE for - light what oh s--- it was a slash pip somehow okay let's just check pip okay okay so if you want to follow along you can it's pretty lightweight package if you don't want to follow along just watch that's totally fine too but basically my goal here is to just show you how quickly and easily you can add someone else's app so anyways pip install Django this is basically it's just something that's gonna override certain model pipes so what we're going to override here is the like this text field that we've got and we're gonna turn that instead into like an editor so okay so we've installed Django tiny MCE for light great so we have that now what we need to do is add this to our installed apps because we want to call upon it we want to be able to point URLs towards it and so on so first thing we're going to do is stop trying to edit in here go to our little sublime here and we're gonna go into my site and settings dot pie come down to installed apps and I'm just gonna add it down here and I'm going to say tiny MCE is the name of the app so the next thing I'm going to do and by the way if you are curious about it I do have put a link to the text-based version in the description and you're definitely gonna want to start heading that way really quickly because I'm gonna make this huge copy and paste that there's no way I'm gonna write out on a video boom there's the copy and paste so these are the configuration settings for our tiny MCE widget basically it's all the things that we want included here one of the things that I want to like be able to click on buttons and all that kind of stuff so anyways that's there cool so there's that and in the so if you go to a text-based version of the tutorial you can just copy and paste that snippet also I link to all of the config possible configurations so if you wanted to kind of change this around this is just one of the more default ones the next thing we would need to do is be able to point our app to tiny MCE because our widgets are gonna call upon tiny MCE to work so we're gonna come down here and we're gonna say path and we're going to allow the path of tiny MCE and basically that's going to work with include tiny MCE dot URLs this needs to be in quotes cool at the comma so I don't forget later on alright so now what we want to do is hopefully save that let me make sure we're not hitting errors yet good it looks like everything is working up to this point now we want to do is head over to our admins so in main admin dot PI now we want to do is first we need to make the import and what we want to say is from tiny MCE widgets we're going to import the tiny MCE widget also from Django dot DV we're gonna import models because we're actually going to just modify the model here for admin purposes so we wouldn't want we don't necessarily want every text field to be an editor right across our entire website we just want the this one we just want one to be so what we're gonna say here is basically under our field sets we're also going to make a quick change and we're gonna say form field underscore overrides equals and then we're gonna say models dot text field that's actually a title cased text field and that will be a widget widget and that's tiny MCE oh my goodness there we go okay so that will override that that specific text field just for tutorial admin for us we could do the something very similar in our models and all that kind of stuff there's there's other ways and like so our for our views to display to like a regular user like right here we're making all these changes to admin dot pi which is kind of like its own separate entity although some people have I've seen people use admin and they like extend the admin to actually be their main website it's kind of not suggested that you do that but you you guys suppose you could but I'm just letting you know this is not the only use case for tiny MCE or any app for that matter or any widget that you want to grab that happens to be an app and and so on anyway save that come over here let's make a refresh darn it I can't get through a tutorial without something like that [Music] let's see what did I do wrong this probably means this needs to be I wonder did I get sometimes that autocorrect smacks me in the face we're not really autocorrect but the suggestion I just pretty confident I didn't make that mistake thanks anyway like sometimes it suggests then I hit enter to like make a new line or whatever and yeah yeah alright anyway long story short boom we have an editor now awesome great wonderful okay now so like for example we could say like print tutorial okay and then we come down here and you know you could write stuff here's how to print things things and then like you can even enter like a code snippet so we'll just do this and we'll define print stuff and then we can pick a language will go with Python print and then we'll underscore here cuz that's proper the thing and grant the thing also will return the thing because why wouldn't we brand stuff awesome so then we've entered a little snippet boom save that boom now we've got two tutorials holy moly [Music] okay anyway alright so we've got these tutorials and now we want to be able to display them and stuff and we definitely don't want our homepage looking like that anymore so now we're gonna start moving into is like views and templates and stuff like that and getting our website to look a little better and then we'll get very briefly I promise into some design and then back to other janko's stuff so that's it for now quick shout out to my most recent channel members we've got KB jagdish Sean Ajay Kumar and robo-one one seven three five thank you guys very much for your support you guys are awesome that's it for now questions comments concerns whatever feel free to leave them below otherwise I will see you guys in another video
Info
Channel: sentdex
Views: 67,206
Rating: undefined out of 5
Keywords: django, web development, website, tutorial, python
Id: BJfyATa9nX0
Channel Id: undefined
Length: 22min 16sec (1336 seconds)
Published: Wed Jan 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.