How To Add Article Snippets - Django Blog #22

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John older her from Kota be calm and in this video I'm gonna show you how to create an article snippet for a blog with Django and Python okay in this video we're gonna look at article snippets 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 go to me.com where have dozens of courses with hundreds of videos a teacher to code use coupon code youtube wanting to get $30 off membership that's all my courses videos and books for one-time fee adjust $49 which is insanely cheap okay so let's take a look at our blog really quickly in the last video we created this rich text editor so if we go to edit a thing we've got this you know this whole rich text thing and it allows us to add images and the way we've got our blog set up whatever our actual blog post is say let's click on this one for instance the first you know however many characters couple hundred characters or what shows up on the main page of our website right here so for here like you know this right here and for this one it's the sandwich now this is not great right if we're printing images or something some other sort of HTML in our blogpost we don't necessarily want it to show up here on the the home page right so to fix that we need to create a whole new field in our database that has an article snippet that we can write for each article so whenever we create the blog post or article or whatever we can write a little you know two sentence snippet that will then show up here on the home page and that's what we're gonna do in this video so this is actually really really simple and probably don't need a video for it but sort of the next thing we need to do so figured would go ahead and knock it out real quick this morning it's Tuesday nobody wants to work that hard on Tuesday anyway so let's head back over to our code and let's look in our let's see the blog directory and let's find our model spy and this is where we've got our post model defined and we've got our title title tag author body post date category and likes and we did this body thing in the last video so basically what we didn't need to do is just create another field for our post model here so let's look through here and let's find one that we can just copy and this one is a car field which is what we want character field and as max length of to 255 which is probably probably what we want think how how many characters are in a sentence that's probably good and we've already got some default stuff so let's just copy this whole thing and paste it in again and let's call this I don't know snippet call it anything you want now we need to give it a default now why do we need to give it a default well because let's see our blog already has a bunch of posts and none of them have snippets so in order to push this migration we have to say well if the blog post already exists and it doesn't have a snippet let's give it a default snippet right so we'll change this as soon as we push the code the first time but for the first time we have to define it so we can we could put anything we want here let's just say click link above to read blog post I thought that I don't know whatever you want doesn't really matter so okay let's go ahead and save this now this is sort of a major change to our model and we need to make a migration and push the migration whenever we do major changes to the model and we did this in the last video so same exact process just head back over to our server that's running and ctrl C to break out of here and make sure you're in your simple blog slash a blog directory make sure your virtual environments turned on and we could just go Python managed up I make migrations and it's plural all one word and this will make us a little add field to snippet post migration now we just need to push that migration so Python managed PI migrate and that's done so now we can run our server again and if we head back over to the website now and hit reload nothing has changed because we need to now make an update to our home page here so step back over to our code and let's go to our the blog slash templates directory and let's look at our home data HTML and this is what we have so far we've got this body and it's given a slice of 200 characters and we've got HTML on so it works so basically we just need to come in here that's create let's go post dot snippet okay and this post us snippet the snippet is obviously coming from our model which where did it go there it is which is just what we named this thing here so whatever you named it right here you just put it right there so we can actually get rid of this oops because we don't need it anymore so delete okay so let's go ahead and save this head back over to the website click reload and boom now they all say click above click link above to read blog post and that's it now we need to make some new changes more changes if we want to edit this thing you can see there's no field to edit the snippet also if we want to add a new post there's no field to add the snippet so we need that we need to do that as well but before we do that let's head back over to our code and let's go to our models dot PI file and I'm gonna get rid of well actually I'll just leave it here and we'll look at this in just a second so let's first do the add new article page so let's add post right here and you can see we've just created the form form as P so we can't change it on this page we have to do it in our form stop PI file so let's look through here and find our form stop PI file in our the blog directory and we can look through here and these are the fields that are our thing currently has so after body I'm just gonna add snippet and then down here we need to add snippet as well so that we can bootstrap fi that box right otherwise if we just save this come back over here and add a post it'll show up but it shows up as just this and we don't really want that so let's see what we can do is we want this to be a text box so so we'll just copy this body line of code and let's paste it in again but instead of body let's call this snippet which is what it's called and it's a text area and we want to give it a class of form control so if we save this now and head back over and hit reload boom now this becomes a box and but now notice it says inside of it by default click link above to read blog post and that's because we set that default text in the model every time we try to add a new post that's gonna be there so we don't necessarily want that so let's head back over to our code go to our models dot PI file and just come back to our snippet and let's just get rid of this default text we have to do it the first time we make the migration or else we'll get an error but after that we don't have to have that right so we just save this now we don't have to make a migration that's not really a major change to the structure of the database itself right so we don't have to make a migration or push your migration this will just work like this so if we save this and head back over to our page and let's go back to the home page let's add a new post and now the snippet field doesn't exist anymore so okay that works there we think we can well let's just try it out let's make a new post this is a snippet post and title tags snippet live okay and let's put it in the coding category this is a test post to test the new snippet thing a mob all right it's not even a word let's make this bold just for fun and here's snippet let's just say testing out the snippet now this is what will appear on the home page if this works so let's post this and here it says testing out the snippets that works so we click on this this is a test post if that's the new snippet thingamabob and now if we click Edit there's still no box here so we still need to fix that so let's head back over to our code and let's go back to our form spy file and just scroll down to our edit form and same deal title title tag body and now let's add also snippet and same deal we'll just copy this body thing right here paste it in again change the name from body to snippet save it reload boom testing out snippets we can now edit this thing and that's all there is to it so alright you might want to come through here if you have got other posts and edit them to change the default from click link above to read the blog post which is kind of goofy I'm but yeah we're good to go now so so okay we are coming right along with our Django blog here and it's looking good 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 which really helps out the channel I really appreciate it and check out coding me calm or you can use coupon code youtube one to get $30 off membership they pay just $49 to access all my courses over 40 courses hundreds of videos and the PDFs of all my best-selling coding books join over a hundred thousand students learning to code just like you my name is John elder from coding me calm and we'll see in the next video
Info
Channel: Codemy.com
Views: 6,505
Rating: undefined out of 5
Keywords: django blog snippet, python django blog snippet, django model, python django blog app, django blog post descriptions, django article snippet, django blog post snippet, python django blog post snippet, blog post snippets, blog snippet, django blog tutorial 2020, python django blog, django blog app, django blog tutorial, django blog project, django blog, django blog app tutorial, django tutorial for beginners full course
Id: NxDNS6cHvUY
Channel Id: undefined
Length: 9min 40sec (580 seconds)
Published: Tue Jun 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.