Add Blog Categories - Django Blog #12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from Cody be calm and in this video we're gonna add categories to her blog with Django and Python alright guys like I said in this video we're gonna add categories to our blog but before we get started if you like this video want to see more like it be sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm and check out coding me comrade dozens of courses hundreds of videos to teach you to code use coupon code youtube one to get $30 off membership that's all my courses videos and books for one-time fee just $49 which is insanely cheap alright check it out horrible scar basically gone it's been less than a week I guess 6 days 7 days I guess a week exactly and pretty good so no more hideous scar on the head you remember I turned and bumped my head on a tree branch in my yard last week and had a big gash on my forehead but it's gone now and it's all cool so in this video we're gonna add categories to our blog so you've got a blog and you want to categorize your blog post you know these are about sports these are about gaming these are about entertainment these are about knitting whatever you want we want to be able to break our posts apart into different categories and so that's we're gonna start to look at in this video and this probably take us a couple of videos it's not hard there's just a lot of stuff to it so let's see first let's head over to our blog real quick and just make sure we're still running okay that looks good so when we log in real quick here so when we add a post what we want is a little drop-down with a list of potential categories that we can select from and then you know that will determine how these are grouped in the future and then eventually we'll create different web pages to show all these sports posts and all the gaming posts and all the Knitting posts or whatever and so that'll be fun so let's see first off let's head over to our models pie file and take a look at our current model right we've got the title title tag author body and post date and we might want to add a new a new thing here eventually for like a snippet or something you know so that when we look at the main page here instead of this just taking the first 200 characters it'll take the little snippet that we actually type in maybe we'll do that later just take a second but now let's think about how we want to do this categories thing first off we want each of these blog posts to have a category field so that we get to keep track of which category we've chosen so let's just do that now category equals and we can really just just kind of copy one of these now since we've already had a bunch of blog posts already we need to set a default just so that those ones we've already created will have something and one of mine is going to be coding so I'll just put coding as the default or you can put under eyes or something write whatever you want if you're building this for the first time and you don't have any posts yet you don't have to do this default thing but like I said we've already got a bunch of posts and we need to give them a default category so I'll just put that as coding and you'll notice I put models doc our field max link 255 and that's good and I just call it category so that's good there but we could do this a couple of ways we could just hard code these in to our back-end you know if we have three categories we just want to hard-code them in that's probably not a good idea we actually probably want a new model so that we can add edit and remove these categories in the future whenever we want so let's just go ahead and build that now so let's go class and let's call this category and this inherits models dot model and I could just kind of copy this and instead of title let's just call this name the name of the category right and we want these same things that we have in our post you know so that we can put it in the adminserver sit if we want to do stuff with it so let's just paste these in and instead of self dot title this will be self dot name and we only have one thing so we don't need the rest of those and that looks good so okay let's go ahead and save that now every time we create a model or change our model or do something like that it's always a two-step process we make a migration and then we push the migration so let's go ahead and head over to our terminal and ctrl C to break out let me just clear the screen here and we want to go make sure you're in your C simple blog a blog make sure your virtual environments turned on if you LS make sure you can see the manage pie file so you know you're in the right place and then we could just go Python manage pie make migrations and okay and it's created a model category and then added that field to the post model and now we can just go python manage pi migrate okay and let's run our server again now check it out if we head over to our admin section you notice the post is there but the category is not so we need to make a change to our admin dot PI file right here remember we we did this way back at the beginning of this thing so now we also want to import category the name of that model and then here we just need to copy this and do it again with category if we save this now head back over here and hit reload boom we see this category and it puts an S on the end of it it's kind of funny looking category this is no big deal so here we can actually add categories right from here and just getting started let's just do that so the first one was coding so I'll just type in coding here click Save and let's add another one let's say sports say entertainment whatever so now we've got three and that's probably good enough just to get started now eventually we will create a page on our blog that we can add and edit these categories from there but for right now just getting started this will work just fine okay so it's head back over here and now when we add a post we want a thing to pop up here with a drop-down that shows all of our different categories right so let's work on that now so let's head over to our Otzi let's look at our forms dot pie file this is our this is our form write our post form which is what we want and we'll probably also need to mess with our edit form too if we want to change the category in the future so let's come down to our field here and where do we want this maybe right after author let's go category okay and then here right after author down here let's make another one of these and this is a gonna be a drop down just like the author tag so we just copy and paste it and it's a select all right so forms dot select and then instead of author let's put category and that looks good okay so let's save this and now if we head back over here and just hit reload we see here a category and we have the drop down but there's nothing in it right so we need to actually put stuff in this and we can do this a couple of different ways now we could do it we get hard-coded and I'm I'll show you how to do that just in case that's what you want to do now we're not going to want to do that but this will work so let's just create something called choices up here a variable and this is going to be a Python list and inside of here we can put our choices now we have three and so we have three tuples inside of here the comma just for good measure and inside of here we can just hard-code what we want so one was coding and now we have two but we have to put them double that's just how select boxes work so one is sort of the way to remember what it is and the other one is the thing so when you do select boxes usually have two things like this so coding the other one was what sports I think sports and then the other one was entertainment and enter oops there we go entertainment okay so we've got this list now we could just pass this in in our category so we can go choices equal choices comma and this choices right here is this thing here so if we call this cats for categories then it would be choices equal cats right but it called it choices so we'll just leave it at that so now if we save this and head back over here and hit reload boom we can see coding sports and entertainment or on there so that works like strictly speaking that's fine and if you want to do it like that that's fine but this is not great because this is not dynamic now if we add another category it won't just show up here because we've hard-coded this so we really don't want to do it this way but it was important for me to show you this so that you could at least see this format because this is the format we need to use and when we put our choices in here also keep in mind here we're putting choices first before the adders if you put it after the adders you're gonna get an error so it has to go first so just sort of keep that in mind so okay so if we're not gonna hit hard code it how should we do this well let's come up here and from models import our new model so that was category oops there we go capital and then we can just create a query into that model to grab those things so we can go choices or whatever you want to call it equals category dot objects dot all and then we want to grab specific a specific list of values from there so we can call values underscore list and then just pass in what we want so we want two things we want the name and the name right so we're just gonna call this twice out of the database because it's just easier this way there's probably a million ways you could do this but I was thinking about it and I thought yeah this would probably work and this this name name mirrors this sports sports and coding coding and entertainment entertainment and the reason why we're calling name is because in our models we called that name right so that's the only thing in this model in this category model is the name of the category so okay that will work I think now this will pull out a whole string of things in fact we can look at this let's go to our title tag here and let's inside of our adders let's create another one and call it placeholder and that's going to be choices so if we save this this will just show us what's in there what this thing is returning it's always a good idea to see what your query is actually returning so save this and head back over here now if we hit reload we see here's what it's returning a query set with coding coding sports sports entertainment entertainment which is exactly what we want and it's a list we could see there's bracket around it which is again exactly what we want so that's really handy so well let's come back here and get rid of that we want to keep that in there I just want to show you what it's returning because I find that interesting and it's useful too so now we've got an object and it's got a list but if we want to put those things into the actual select box the drop-down thing we need to loop through there and create a separate list that's not an object just so it's easier so let's create a list and I'm gonna call it R or let's just call it choice underscore list and this is a list and as I'm gonna set it to nothing right it's just an empty list so now we can do a loop so we can go for let's say item in choices which is this object right here that we just created with the query set right we want to choice underscore list dot append item so what this will do is it will append it will add on each of these things in our object one at a time as it loops through and now this choice list will be an actual Python list instead of a weird query set object which brings all kinds of trouble for us but now we can just take this choice list and bring it down here to our category and instead of doing choices here we can do our choice list all right so category choices equal choice list so go ahead and save this and now if we head back here and hit reload boom this is coding sports and entertainment and now this is dynamic and we can tell by let's go to another page here and let's go admin let's go to our categories and let's add a new one and let's say this is gonna be motorcycles or hiking I like hiking so we add this category boom now we see there it is now if we come back here and hit reload this might not work yeah we're gonna it's always a good idea to for some reason you need to restart your server when you do this so let's restart our server I'll come back here and hit reload and now we see hiking is on the list so okay that's cool so now it's dynamic now if we create new categories they'll just show up which is exactly what we want to do so we can now let's create a page for that so we can do it from the website so let's head over to our view spy file and let's see our add post view let's just copy this real quick and create another one here instead of add post view let's call this add category view and instead of the model post this is gonna be model category and let's not even use a form class because it's only one field so let's just designate our fields to be all and for our template name let's call this add category all right so let me copy this and let's save this and now let's kind of work our templates and create a new file and let's go file save as we want to save this as add category dot HTML now we can go to our add post and just copy all of this to our let's see we're to go add category thing and instead of it saying add post let's say add category and down here instead of post let's say add category and we're gonna still use this form as PE so that stays the same and create a new blog post let's say create a new blog category as the title so save that now we need a view or we need a URL so let's go back to our view and grab this the name of this add category view so let's copy this and let's go over to our URL spy file and here like all the others we need to import that guy so add category view and then down here let's grab our ad post and oops copy this and paste it in but instead of ad post view it's ad category view instead of add post it's let's say add category and instead of it going to add post let's send it to add category all right so let's save this and then finally let's add a link to this in our base HTML so let's head over to our base HTML there it is and let's look down in our nav bar and if the users logged in we want them to be able to add a post or let's just copy this whole thing and that it's instead of sending it to add post we want to send it to add category and instead of saying add post we want to say add category all right so let's save this now let's head back here to our main thing and must have done an area or somewhere edie category is not defined where let's see views dot pie okay so we need to import this and I've used up high so let's run our server again and what did I miss did I bring it yeah up here for models import I didn't import our category model so okay that should work because down here we're setting our model as category so obviously we need to import that I just forgot to do it okay so let's head back and now let's hit reload and we see this add category link up at the top if we click on it boom we can create a new category now let's call this one motorcycles and when we click here it read turns us back to the home page and if we add a new post it doesn't show up but if we restart the server just kind of a quirky little thing here and there's probably workaround for that so you don't have to restart the server but I haven't looked it up yet so now if we hit reload boom motorcycle shows up and that's cool so let's create a post let's call this my hiking post and the title tag is gonna be hiking and the author is me and we want this as hiking and this is my hiking post boom here we see my hiking post but it hasn't been categorized so we don't know let's head back over to our home bow and if we look at the site here let's say right after the title let's put the category so that would be right here let's put another - and this is just post dot category so let's save that come back it reload boom it says hiking and the rest of that coding because by default they were given that when we made our migration earlier so okay so our categories seem to be working we've got a add category page we haven't done an edit category page but you can probably figure that out on your own it's just like we did the other edit post page only instead of editing post we're editing category so no big deal there actually this video is getting a little bit long so maybe we'll do that real quick in the next video okay just that easy to add categories now we haven't created any URLs so that we can have like a category page that shows all the posts from a specific category we'll look at doing that next but like I said this video is getting a little bit too long so I think we'll wait and do that next time so that's all for this video if you liked it be sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm and check out co2 Meachem or you can use coupon code youtube wanting to get $30 off membership so pay just $49 to access all my courses over 40 courses hundreds of videos in the PDFs of all my best-selling coding books join over 100,000 students learning the code just like you my name is John elder from Kota b.com and we'll see in the next video
Info
Channel: Codemy.com
Views: 22,130
Rating: undefined out of 5
Keywords: django blog categories, django blog category, adding categories django blog, add categories to django blog, add blog categories django, python django blog categories, python django blog category, django blog adding categories, django blog tutorial, django blog project, django blog app, python django blog tutorial, python django blog app, python django blog, django blog, django tutorial, django categories tutorial
Id: _ph8GF84fX4
Channel Id: undefined
Length: 20min 9sec (1209 seconds)
Published: Mon Apr 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.