2025-02-03 | Code With Clinton | Django Rest Framework Crash Course 2025
构建REST API与React全栈应用入门教程
标签
媒体详情
- 上传日期
- 2025-06-21 19:10
- 来源
- https://www.youtube.com/watch?v=Mj3dGdBdiO4
- 处理状态
- 已完成
- 转录状态
- 已完成
- Latest LLM Model
- gemini-2.5-pro
转录
speaker 1: Hi, welcome to my rest api series with Jango and Jango rest framework. So this in this rest api series, you are going to learn how to start building the rest api by yourself comfortably instead. So once we are done with a rest api series, we are going to go over to the front end with three appso. Are gonna to build a blog application with inststanwe. Are going to build a reappfront end that's going to simply consume the rest api we built with jungle and jungle rest framework in the so so first thing, we are going to build up our apis. And once we are done with the api series, we'll go towards the front end. We are going to build an exciting web app like this, which reacts. And then and then we are going to simply integrate the react app and our jungle api together to form a full stock ack web application. So going to be an exciting tutorial. It's going to be an exciting time here. So with us for that ado, let's get started right now. Okay, so we are going to start on the ground up. We are going to start from scratch. So first and first and go down open and get my command prompts. So I'm going to search for that cmd. So here is its command prompt. So click on it, see if you're on windows, it's called cmd. If you're on mac scoseminar, right? So currently tly with my command proi'm going to go into my favorite folder where I create my jungle project. You can create, you can create yimage Hever location you want to okay, I'm going let's I'm going to say cd jungle on the scope projects. Jungle on the scope projects. Jungle on the scope projects. That's my own directory. Yours can be called some other thing instead, so you can create your jungle features in whatever location you want. So you can do that on your desktop or your documents whichever place. But I'm going right now I'm doing mine in a different location. So I'm gonna to sorry, I'm going to say see the documents documents first see in the document direct you have a folder called jungle underscope projects. So damina so the first and face it its a cretive virtual environments in so this like right I'm going to show you how I create my own virtual environment. I'm going to say deep install peep install virtual I think it's peep install virtual env rapper I think win so it's necessary to score people instvtual env rapper ifinwin its enter so right I'm trying to create my own virtual environment. You're gonna so you are going to run this command once on your computer. It's going be run. It's going be run once and understand. So let's wait for it while it's creates the virtual environment. Rapper, so currently says requirements already satisfied. That's because I already have this setup file installed. So right now, this this right now this file, you see, opa is the setup file. So right now I'm going increase my virtual environment. I'm going to create the virtual environment ments. Now I'm going to say make virtual, make a ftual env. I'm going to call, I'm going to just say, I'm going to say rest. I'm going to call my virtual, I'm going to call my virtual environment rest api on the scemv. So that's the name. So the command is make virtra emv then the name of your virtual environment. So I called my rest api emv rest on the score api on the scemv. So it's answer. So those was . speaker 2: for . speaker 1: so right now I just add my virtual environments created and activated can see right now you can see this yes my virtual environment ments you can see here it's also both created and also activated in so this parentheyou can see it also parenthesis by the side that shows that your virtual environment has been activated so right now we have to install jungle inside the virtual environment I'm gonna to say people install Jungo Yeah its enter so because your virtual environment is activated jungle is going to be installed inside of it you get so when to say people install jungle going to take it's it's going to take it's going to take some seconds to install well let's wait for it I'm going wait for it. So right now jungle is being installed. You can see it's sticking. You can see a trying to install jungle is some other packages that Jungo might need instead. So it's not take quite some lesswords for it. Okay. So right now we have jungle successfully installed into our virtual environment. Can see I successfully installed ahgf and Jango and some other packages, right? So right now it's time for us to create ates our jungle projects. I'm going to say, I'm going to say jungle jangle admin start projects going to call, going to, let's say, I'm going to say new jangle api. Okay, okay. I will say new jungle api. So that's name of our jungle project is called new jungle api. So its answer once you want to command its answer, so our jungle project is going to be called new jungle api is be and and it's going to be folder to now say cd new underthe scjungle underthe scapi. I'm going to go I'm, I'm going to go into the right now I'm trying to go into the folder into the project I just created. I'm trying, I'm trying to go inside the jungle project I created. Right? I'm going, I'm going to eat answer right now. Right now I'm inside. Okay, and then I'm going to open this directory with vs code. I'm going to say code, then dots. So I'm trying to open the directory. I'm trying to, I'm trying to open our jungle project with the vs code. So I'm going to say code dots its answer. And this command will open this jungle project on vs code. Right now, vs code is life is called. Right, ten now yes, got this up. So you're gonna do as simple. You're gonna see yes, I chose the authors yeso. I'm gonna make the font size big. Yeah, so right because now we have our jungle through justice code new jungle api that's our jungle through that, right? So I'm gonna open our examminal gonna a examminal ar on vs code, click on those three ellipses, click on seminal ar new seminar. So I'm gonna to close under those spots bring this down a bit its when to open our command prompts. So now we have our command from on vs code. So now we have our jungle project. So I'm going to create a separate app for our jungle project and I'm going to create and I'm going to create an app for jungle projects and right now you can see now we have a virtual I'm trying to reduce right now can see we have a virtual environment already activated for us that is good. So far we not do so. Want to create an app for jungle for for our new jungle project and app. And the name is new on the scjungle on on the scapi, right? So I want to see Python manage or spy manage to spy starts up. And I'm going to call this up. I'm going call this, I'm going give my uin name. I'm going call this up I just say blog up blog up yes its answer. So if we check those now you're gonna to see a new folder. You're gonna to see a new folder up here. Yeah so right think as you have the block g app it has been created now so I'm going to do is simple once you have your blog once you have your app created, go down see your jungle project now member our Jungo project is called new Jungo api. I'm gonna open the folder go down to the file called cby file. So in this file you just screw down and you find these installed apps. So we are going to add our new app. The app is called blog up blog up. So right now we have our jungle projects life so we have our jungle projects created for us. We have a new app created set. We have already registered our app. So what we are going to do next, so what I want to do now is this. I will simply customize the jungle user model. Understand? By default, Jungo user model comes with some fields. For example, it comes with the user name field. It comes with the face name field, comes it comes with your email field. It comes with some other fields, right? But if you can check for what we have on the front end, right? Can see right now, can see these are our front end application that we intend to build later after we are done. The rest api, you can see we have this user, this user as he has, he has about you, he has is whening yoalso as social media url is social media links by you. He has job title too. He has, he has a social media url. By default, jungle user monitors not come with these fields. So that's why we need to customize jungle user model to accommodate the biofield, the social media field and then the job types field and also the profile picture standwhen we ask, that's why I want to customize jungle user model. Okay, so right now, let's customize the user model instead. So I'm going to go down to my models by file. Go down to your blog up, you find the file called models of five. Support the file. So right now want to create a new user model instead. So we are not longer going use the default user model that comes with jungle to create our own user model, right? I'm going to just say class. I'm going to call it custom user, custom user, right? And then we are going to simply inherit from abstracts user abstracts user abstracts like on spell that abstract user White malimport abstracts user. I'm going to save from from Jango docontritrip dot . speaker 2: contritrip odds . speaker 1: domodels imports abstracts user Yep. So I tell before Jango so right now it abstracts uwe are simply trying to rachannel channel simply updates what Jungo already as channel we are trying to add more fields to what Jungo already as by default and understand right now I'm going to add the following Fieland say biyou actually tell you now we are trying to add more fields to jungle already as understand stuff that so that what are trying to do it I'm going just add I'm also add the bio field I'm going say models of text area rice is a text area I can't even spell this again text area but I'm correct text area and I'm just say blank should be I'm trying to make it not compulsory blank should be and null should be called and then I'm going to add these right nouns are these social media links okay let's right now Oh this is mine this is called text field no text area sorry I don't know text field Yeah scotext field now so I'm going to add shamedia link let me just speak to line here so this is for Facebook so you can see right now isn't there in a url field right so we also have to set blank to not to choose so I'm going to add the other links so let's add . speaker 2: for YouTube . speaker 1: let's ask for. Instagram Instagram unless let's ask for LinkedIn. Oh, let's ask for Facebook. YouTube, let's ask for tter. And lastly, I think this far enough. Yeah, enough. So also also add the field for the profile picture. So common that the bio field and the c five picture should be equal ed to essay models. That's image field. And then we are going to add the only to say upload to I'm going we going to upload to a folder code. This should be I'm going to simply upload to a folder code cleamy friends ts code profile image, right? And I'll just say blank should be equal to why is this thing obstructing my view? And knowledge should be equal to Yep. So we've got it already. So now we've got this not so right now I don't know, we don't know a string function now just say return soft dot username. Return soft dot username. If we check loosely, we don't have visa name on this. We don't have right now. You can see right now, right? For you are seeing here. We can't see we can't find username here. So what we are doing is we're simply trying to add more fields with the abstract user class. We can simply add more fields to the existing Jango user model. So what you're trying to accomplish here, you and I always recommend that in whatever jungle projects you are working on, is the best plfor you to always customize your user model. It's always best you customize your user model because because you don't know what requirements you might need in the future in a that might require to customize your model at the middle of your projects. So at every project you want to study in jangle, the best way to do is to customize the user model first. Customize the user model first. And that should be the first thing we should do. So now once we are done with this, now right now we need to tell Jango that so inlet jangle know that this is our new user model, right? See your see your same by file. So the same what by file you scroll down to the bottom. I'm gonna to add a new field. I'm going to just see oduser model. Oduser on the score model should be equal to our app is called log up a small letter blog up dots custia right am I correct? Let me block up dots okay it's going be block up docustiser custom user perhaps have so now hope everything works well so now down to our jungle admin so admdle spy file rather admin those spy we U shop we here so right now want to simply register this custom user in standard we have to register it right but so I admin now so I'm going to just say class. I'm going say class custom user admin so we're going to init from going to see from well we have it from user admin so we're going to just user admin user admin you have to import user admin now let me just say from Jango dot . speaker 2: contrib . speaker 1: dot odot admin or import mean I'm really open correct? Yeah so that's it now gonna I'm going say this display this on a scdisplay right? We want to display custom some of our user fields on the jungle ad name someone on right now display the I just say the username, the username the email. I just say the first name and the last name. So these are the fields right now. These are some of the default fields for Jungo isa model, right? So we've got this terms. We have to register now and we have to register this custom user admin and we also have to register our models of by file, this particular model. So back. So I admit that by I'm going save from domodels. Does the current folder from those models imports custom user right there I'll move and see admin . speaker 2: dot sites . speaker 1: dot register register outhe . speaker 2: site that register . speaker 1: I'll just say custuser and then custuser admin. So I'm registerboth the model and this admin class. So once you've done this, now let's make migrations. What's your cmd? C Python minor spy make migrations it ends up. So right now we run into an arrow. Right now it's complaining about our oduser model. It's scene that we that we called is saying that it does not exist. Let's go back 65, back to 55. Right now. Coded block up customer. User is not customer. It meant be customer user. So now we call it, we call it here customer om user and not, I would say I wrote customer. So that was a mistake says customer om user slamic migrations again. Okay, right now it's trying, right now it's trying to also to install pillow. Pillow is a package that sests to undo images in jungle. We check closely we are working right and see now we are working with this image field and that means we are going, that means we are going work with images, right? So we have to install pillow. Gonna see install pillow. So that's a command tip install pillow. So let's wait for to get installed into the virtual a environment. So right now we say successfully installed pillow right, right now we no around the let's right now let's make migration tions this time unless be hopeful, it works. So it's works not so long . speaker 2: as my grates. speaker 1: Okay, I want shudow my Grigon to a super usee up my griis going fine. And let's go to a super user. Going to save pson. Manage, manage your spy. Manage your spy. Create super user. Create super user. One word. I'm going to call my, I'm going say cleanstatusername cleemail address again and I just say cleanat gmail dot com password in something there one to 34 cs and then let's run saver. Saver. Let's go. Come on. Let's on server penpython manage by y run server. Right? So it's answer, so it's open now. So go down your control key and click on this url. Right. Thank ourselves our jungle projects successfully. Our our jungle projects is working fine right go right now we're gonna go down we're gonna say slash admin we're gonna go down we're gonna go into our jungle admin. Yep so we have to log in so these are my credentials so login so we are inside now can see this it is a new usser's table because see is it nice in the blog up model right now we can is all we have right now because we have a new user and this is how we have here is the super user. I just created it right? But don't worry with the festival last time I bought mc you can just click on the user and then go and update dates. The first and last name I'm going just say cleansing matchiku that's my full name and I'll simply just save yeright. Now we just customize my user model now with added some extra fields the model some extra fields like was it called like the bio the bio the picture and the user social media links right? Okay, so guys we just finished customizing the Jungo user models. I'm going to show you how to how you can you can sign up users and also how you can log in users using using simple jwc in so by from our application here once who gives us the ability to create accounts and also log them in understand? So okay, it's right now time for us guest. Right now it's time for stop our rest api proper instead. So it's time for the rest api. So what I'm going to do now is this. So right now we have to install the jungle rest framework package. You have to install the package now I'll open a new command prompts. And down there I simply say, make sure your functional environment is activjust like my own. Our majsay people install and make this a bit bigger, let say people install jungle rest framework, right? It's going to be a single word. Jungle rest frame work, it's answer a single word. So while it is installing, I'm going go down to my statof pfile go down see your settings of by file in the installed apps region. Just over here I'm going to add a new package called the rest on the scope framework. The package is called rest on the scope framework schter ready. Have a jungle reframosuccessfully installed. So box my block up. So on the left panel, click on the left. Click on the left panel. So in your blog, upcreate a new file called called civializer server lizers or spy the sentence this far, we going to simply fit a civilizer for for our custom user model. First face ice, import civilizers. I'm going to say from rest framework, import civilizers, right? So I'm going to say class. Someone will just say right now, the click serithat that will enable users quit accounts, right? Someone will just say user registration civializer. So better you give these classes a descriptive name to make life easier for you. So now what I recommend. So it's called user registration civilizer. And we are going to simply inhabit from civilizers dots model civilizer. Right now we're going to have a class meta. It's just like a model form. It's just like a model form in jungle. We're going for a class method and we're going to say model should be equals to the custom user. So our custom user model, right? Custom user, we can get that going save from jungle. But most times you're not going to use the model name directly. So this is better up. You're going to just save from jangle dot com be that's odds. That's gonna . speaker 2: ship that odds, right? speaker 1: We just say imports get ua model. So this so this is so right now this is so this is the best way to do it then having to use the exact name of your user model it does in sense so back here just gonna tell me just get this guy copy and paste here put some the parenthesis and the fields I need are I need this fields sinthe ID the email the username field this this are the default field that comes with the user model and the last name field last name field and right now over via these are the custom fields that we add that that we added by ourselves that that we add to add by ourselves right in a are the custom fielbut what we are here are this these are the default fields that come into our user model and one more thing also is add a password, right? Because quuser paspassword so one more thing. So now let's add this line just has line. So this line is not make sure that our is going to make sure that our password can only be right only. It simply means that our password is going be eating. It cannot be seen. It will only be right only. Some of us are trying to do it right. So now we just wrote ot of us. We right now, we just wrote ot our user registration serializer, right? So right now we have also come down and customize the create function. Yesterday, I also to customize the create function of this serializer. You going to see Dev creates? I'm going to say solve and then validated data. That's got data right now, get our validated data, which are this stuff here. So if these data are all valid, want to create our user right? If they are all valid, so I do know they are valid. This these are we know gonna say email should be equal to so should the app of this we validated data we are going to know if they are valid or not so gonna just just say we going to save validated data then scrap brackets are going say email are going to do the same thing for the username then for the username the password the first name and the last name right so that is meant to be right so if I do right now this vdata data it all the correct information so you get so right now just go via and I'll just put in the user name make the comment fire and I'll put in the first name and that seems now do basis the last name and you should be the first name right so we've got this now so now those user should be equal to the gets on the score user on the scmodel right so what I is our user now so this get user model simply helps also get the user model we are using it so I'm currently raising correcraising this customier we are currently we are using this customiright so that's what it does so this is the best practice to use using of this get user model then having to use the actual user model that we call the it stanso this is best practice so what. I'm doing now. So I just say, let's a user where I just say new user should be equal to the user. Those objects, those creates. Now just say email should be equal to email. Also username should be equal to username. And then first name should be equal to first name and last name should be equal to last name. So now we just created our user, but we check loothe. Next you want to do use last name should be equal to last name. So right now we just created our user right? speaker 2: So next information . speaker 1: ces Donais to set the password right? We we to see password should be equal to vdated data square bracket password de a password oand over. Just come see new user that sets password de and I'll PaaS sing this password that's it's another new user dot save then me have return this new user. Return new user. So we have done with our civilizers. So was the next place for us to create a view for this civilizer. Us to create a view, a view function for the civilizer. So now I'm going to go down. So I'll block g up. So now block g up. Now we have a new file called visa by file. So over here I will just say the say register. I want to call this v register user, register user. So this view is going to simply cover, it's not all the request arguments. And now we're going to just me just say, so they want to create any user rights. So a zer seralizer seraliser should be equal to user registration serializer. I'm going to copy this, then we paste it here. We have to import it. So if we import it now, after doing this, now we're also gonna, so we're gonna to say, serlaser should be equal to user registration civializer. Whether we are going to PaaS an object, the objects are dispute. Te, this field you have here need threatening. This are the objects on our past to our civilizer, right? And we're going to just say data should be equal to request the data, right? So we've done that. Now I'm going to check if, we're going to say if civilizer dois valid. We're going check if it's valid. If if it is valid, I'm going to simply do what save ved recogninohere lies. I don't save else. And then I'm going to simply withon a response. If don't response, I say it's called response according, then this civiare the data. So so it's not simply return the newly created user data. So it is an error. We are going to simply return a response and then we are going to just say, we are going to see civilizer dots. Arrors then are going to simply return a status message that says, we're going to say status should be equal to status. That's six httsp underscore 400 underscore bad requests. So Yeah. So right now we have to do some imports. We have to import this serializer. We have to import response. We have to say import status. So right now, say from, let me try this trick, control and then dots. So now with you, hold on, control and dots, you can be able to you can be able to import some of the classes and functions in your jangle projects. That's for now. I'm able to import this user registration serializer. So we just got that up there. So now for this response starts coming from rest frame work. Come on, save from from rest on . speaker 2: the scopframework . speaker 1: from reframework. That's response. We have to import response. And for the statue. speaker 2: obviously save from . speaker 1: rest framework import status. And if you check it, remember this is meant to be a rest api function, so it needs a decorator. So gonna just save from rest of the scope framework dot decorators imports. speaker 2: I'm going to say . speaker 1: imports api on the scope view. So I'm going to add the orate. So I'm going to say adapi on the scope view. So so this decorate so is going to simply it's going to only accept a post request right so we've got the function created now source next team the next focode creates a url for this view so we going to our conduct next so right now it's time for us to create a url for this view so we're gonna to our blog up again I'm gonna create a new file say new file going to call it urls dot spi it's answer so you have the file created now I'm going to say from Jungo url we are going to import the parts from Jango urls imports parts right now I'm going to also import our views mostly from the current voter import views and right now I'm going to Yeah let make a mistake I'm going to see url patterns I'm sure you guys are familiar with these things already someone of your say parts now just call the the url is going to be called register you underscore user now say faslash I'm going to say views doregister user and then just give it a name just say register underthe score user I love giving them the same values right so now they all have the same values it's not immense they must have the same values so as they've done this now next if we do is simple we have to simply I simply let Jungo know about this. Else by file because currently our jungle project does not know about this fabric created. So you're gonna go down to your jungle project. So go the jungle project also has a file code. You outload it by here as a file. So it now right now, so let it know about the url file we created for the blog up in the stand. So over I'm going to come away and do that. I'm going to just add a new parts and we just add an empty string there and we just simply say views. Sorry, I'm going simply just say include blog up dot . speaker 2: url statds . speaker 1: itself to import include from jangle dot url's imports include. So we've got every tv one sets, we've got our url sets, we've got our views sets, we've got our civilizers, our views, we've got our civilizers, some oas sets. So the next thing is we also go and try out this thing if we also try out if also go and try out creating a new user and see if it works remember using an api. So if also go and try and see if we can create new users now let's see so saver is currently running so if also tried to going to use postman so versus code as a beautiful extension called postman don't see your vcode go see a versus s code extension. So click such a postman postman command or wamy time you can see we have post one the the the first command the first one you have to make sure you have it installed you it make sure you have it installed once it's installed you're gonna sit on the you're gonna sit on the left it's gonna be on the list of items Yeah currently my I'm gonna click on this three ellipses I'm gonna see yes my postman so you can see this the icon this of it looks like you can see it's up here now so these for you your icon might show up here by default the reason why my own icon are here because my some that's because my window is zoomed in so yes are the iconlike so I've told you how you can get your postman I'm gonna click on new requests click on this new request but this new http request click on it so it's loading up. Okay come on, come on, come on slow then. So and try out and see what you've done so far if it is working or not. I'm going to reduce the size of my Yeah hundred reduce it a bit. So that's what we are doing. My server is running around currently on top 8000 since my postis opened. So for for you to create a new user, it's simply open puts a jungle url, which is one to 7.0 point zero points one and the port number buys 8000. So right now once, right now we want to register new ser. Yes, the where is it again? Yes, the the url register user. So copy copy back to my postman. I'll paste it here. This is the url. And this url supports only a post request. Remember, from the visa by make the some of these guys that . speaker 2: we don't need. So from the views . speaker 1: up by now you can see all supports a post request ests you can see there. So why am going to click on this job then I'll select posts so I've got my url so because also because your url is once White and one straight to my user right? So click on body and click on the body tab click on leads and then choose form data right? So right I want to impete our information, which are the email, the username, the first name, last name and password. So the email also, once click on form data come down here, I'm going to say email. I'm going to . speaker 2: just say tests. speaker 1: I'm going to justijust say, I must say, okay, let me just say done at jimedocom. As for the uname, I'll just let me just say Daniel, let me just say for the first name post on scname, I'll see Daniel this let me just call Dan. Let me just hear then for the last name, that's a son name, I just say Dan, then let's say done Mike as his last name. And lastly, the password. PaaS mode or just okay I'll just I'll just say okay we might just . speaker 2: put something . speaker 1: option remember something like compute and I just say backa on Yeah baka andi mean baron a fso that sso this are the remember these are the objects we where we are asking for the email the username, the first name. Last name on password right so now I've imppeted those values so was next next right now let's click on sent and let's be hopeful that she does not return any error. It's looking like we are already running in suan. Okay, now click on so let me use the size of this stuff. So right now click on the send button on top. Click on it. So if everything works, that means we can create usas comfortably. Let's hope it works right now says 200. Ok, that means it works. So I'm going to raise this up. Let's raise it up right then. Consider we newly created user del, remember, consider we have a newly created user perfect. Couldn't see my jungle admin if I refresh this place. Refrenow, you can see we have Daniel creator. So right now we can now create new users successfully. Okay, so right now let's work on login the users instead. So unfor login users, we are going to use a package called simple jwc. So what's your browser look up? Simple jgon na say simple jwc. So Yeah, the first link, click on . speaker 2: it . speaker 1: sevnow, so here is a guide for you. So click on getting started. Click on the first link getting started. So we have to install this package called jgoas framework. Simple wso. Copy this command. Copy up to your jungle code. So just raise the examminal up. So I'll paste the command there. Show happening. Then I'll copy it. speaker 2: So let's . speaker 1: copy it again. Copy this command. Please to say each enter okay also why installing now let's keep going to the let's keep going to to the documentation okay what else do we need to know? So we have to copy this copy this code down your down to your session dot five file copy and paste it in your settings by file Yeah so now already else simple put your wc already installed so now let's copy this command let's bring this guy Sparto overyour sfile there is my so we are going to copy this command copy and then fix it in your in your by file in your session by file. So come over here. So once you paste it they remove these ellipses remove these three dots, remove it remove this one and this one. So this simply tells our jungle this simply say this simply means that we now switching over to simple jwc for authentication. What it means get so that's what it means. So once you've done that, so you're going to copy these two parts, copy these two parts now copy them and over to your projects. You are able to spfor you're going paste these two these two parts, right? So just copy and then your projects you already by these are for you . speaker 2: listed here. So I mean. speaker 1: you you approach your Arby y file that's the file that's you the spin your jungle project. So you're going to paste it this. So I'm going to simply, I want to switnow remove this api slash you move the api. It's not emotional. Remove it. It's not emoremove. It's right subgoing. Rename this to talking on a score refresh so that better bethat. So Yeah, Yeah, we've got this now. So via, we have to also import this. We have to also copy this imports. Copy these imports, copy it and then we have to paste it just above of Yeah Yeah and this is all we need to do and this all we need to do right? So once you've done this now it's time to work on the login you get so down to so over to your server to postman. So this is the these are our endpoints. The endpoints is talking for it to get logged for face which generates the access and refresh you have to send a post request to this endpoint. And you are required to PaaS in your username and password before you can get this toin right. So to vs code, so on postman, I'm going to create a new request. So click on your postman icon on the left, click on it, click on new request. So once create a new, once create a logo right now on to try out the login and see if it is working right? So I'm going to see one, two, seven. So over here I'm going to put saying talking moso. I'm going to replace that with talking. I should be a post request and then click on body and then form data. And right now I'm going to PaaS in the user name. Remember we quitted the user called Daniel. If you remember, jk's name is Daniel. Okay, this one is called done. This one is done, right? So this one is done and the password is backon. Back on dreright. So let's try. So I now click on the send button in as hope it works. John, I'm think it didn't work. I can't even say why. Let's try again. speaker 2: Click on send. Okay. speaker 1: right now think, let's run the server and see what the problem is. My servseems to be, I even know the problem is. Let me just face this guy. Let me close Seron the saver here. My saver is currently let to close . speaker 2: this legenknow . speaker 1: sai again, to be sure. Okay, my son was not running. Sorry, we have syntax error. Sai was not running. So let's see slaon the saver again. Now was the issue run saver. Python means spy. Runsava its answer, crosoa is back again . speaker 2: over here. speaker 1: So your passing username, the password, the endpoint is loo slash token. And then click on send. Right now expecto get right now says 200 okay that means it's so successful right so let's confirm that take this up okay right now can see we've got our refresh token right we've also got our access to king understand so we've got this to now so what's the next phase so then right now user can sign up the can genernaare talking so the next one to do now is I'm gonna show you how to use this toto gain access to to tected routes instead I'm going show you how to use it but for now we can usser can sign the conthe account they can also gets their tokens right so the next thing we want to work on is how how they are going to be how they are going to use this token to access protected route together so working on that next okay guys right now it's time for us to work on the block api fully instead so this block api what are we doing here what what is it what is it about so this block api you can list that block contents you can view the you can view the you can for example you have here you can view the you can I'm trying to remember you can view the list of blogs you can view the you can view the detail. Page of each blogs in a. So you can you can create new articles. You can create new blog articles. The auto of your blog can update or delete these articles, right? So I'm going to work on that in. We are going to concluoperations and we're also going to ask some permissions, right? By only the auto of a block and obbesity block g, only the auor of a block. And also xo deletes this blog if we wants to in so we going to work all over. We are going work. We are going to work on all these features on the back end before we start implementing it on the front end it understand. So so back our code now we're going to go down to our jungle model. So so jungle model. So in the last video I talked about showing you how to use this access key to gain access to protected droughts. So we are going to do that whereby if you are not logged in if you don't have this key, you can't create a blog. So we are gonna work on that now. So Buso code over I'm gonna close I'll will leave those same I'm gonna to open a new I'm sorry, I'm gonna open my Moby file and it's in your blog up in your blog up there a file called models to spso open it up. So you we're back here again so gonna to create a blog model. I'm say class. So what are the characistics of a blog? A blog is meant to have a title two and say models dots model a Blois meant to have titwo, right? And that's going to be a character field models or character field. Now see max length . speaker 2: should be let . speaker 1: me just say two, five, five. If blog is going to have a slog, we're just going to see some models that slog field. Now just simply say, okay, think, think things. I'll say your nicto be and null should be equal to sing. Sorry, not null, but blank should be. Blank should be. And the max slash with 255 oxo, say, Blois oximents of the contents of the body. Let me just say escalate body, escalate contents. Same contents is much more descriptive. Let's say contents. So that should be a text field models, dot text field. A block is omental of the auor. The right side of this blog the also write and that's going to be a foreign key relationship to our user. Mostly models. It's foreign key, right? So it's going to be this custom user. But the best way to do, the best way to do this is to get the user from from the sings of by file. We are meant to reference it from here. That's the best way to go about it's are meant to point to the user from this oduser model. So how do you go about it? So remember, the oduser model is in your settings by file. So you're going to say from Jango. Com, import import settings, right? So we are trying to link up the user and the blog. It starts because a blog is written by a user, right? Arising in following key because a user can have several blogs and a bloonly and a blocan only can only belong to one user, such as say once to many relationa user can have several blog and a blocan only have one user. So going to say models of foreign key, we are going to are going to point this custom user. We are going to do that through the settings that by far I'm going to say settings that's oduser model or underscuser underscmodel member. We have that here. Here we said, okay, then are . speaker 2: going . speaker 1: to just say you're going to say on delete enwhenever deletes a particular llsay on deletes whever whenever you deletes a and also want to simply let the blogs stay. I'm going say models that set null once keep the blogs, models that set on the score null. Now say related name. Want to add the related name. Litered name should be equal C, I simply . speaker 2: just say . speaker 1: blogs and I'll set Nuto. So we've got our, we've got the authso next one also get the dates this blogoes create set. Just say create set should be equal to models dot dates field. Let just say momodels are dates time field. I will say also on the score now on the score, R should be equal to. So what this auto now R does is that whenever you create a blog is in a recorded time, the blog was created. So I'm want to also add the updated field. So whenever the blog was updated once to also gets the time. So going to see models this time, the same fethat we are going to say also on the score now should be equal to so that what this means whenever the block, whenever a change is made on the blog is going to capture the time any change is made on the blog, all this auto now those plus auto now at it simply captures the time this blog was first created. Instalso also have a published field in you can create a blog and not publish it yet. So once also capture the time this user is going to publish this blog. If the blog is not published, it cannot appear on the own page. It cannot appear yet. It cannot only be seen in your profile, cannot appear on the index page. Right? So right now let's I'm going say published, published. So I'm going just say, let me just call this created ads, not just created updated ads. That's more descriptive. Now say published time should be equal to. Now say models dot this time field. So now see auto on the score. Now on the score hard, sorry, sorry. Isn't when I say blank should be equal to and knowledge should be equal to. So I will let you know where we are going record when we are going catch this for bleach time. And the last field is gonna say is drafts. Remember by default if you create a blog is not be such a draft. But last means the blocannot be seen by the public can only be seen by you, the owner of the blog. So if your blog, so by default say models, that's bulolean field, boolean field. And when you say default should be equal to and if I put a block, it will be set second. So the so will set it to it's going to be a draft block whereby it's not, it can't be seen by the public. It can only be seen, may you turn off this a draft field. Understand. Understand. I think it's can be seen by the public. And right now our Blois going be I also also give our bloof categories. Right, right now let's give up load some categories. Our paste this command paste code there. The simple one, we are going to use a choice field, so we, our bloeither fall in the technology, economy, business, sports or lifestyle category in the stanright. So now I'm going add any, you know, say, category should be equal to models tocharacter feed. Now I'll say max, say max length should be equal to, so we're going to say max length should be equal to s 255. I will say choices should be equal als to category, right? And then we're going to then say, what next? What next? We can just say just blank should be and knowledge should be. Ok. Our blog is meant to have a picture. It's meant to have an image, right? Something like this, right? Something like instead. So we're going to add, going to say featured image. Now see models dot image field. And I'll just let me just say upload to. As me just say, block on the score img, and I'm going to . speaker 2: say blank should . speaker 1: be and now this should be equal to. So I think we've got all we need for a blog. We've got everything. So we are going to add a class mesay, class mewhere. I want to simply order a blog by the published data. I'm gonna say order now say it's going be the thing. It's called ordream should be equal to. So right now, to make sure that the blog that was published last sts a ps fists, whereby if blog was published last one, make sure that block a ps fiinstead. So right now what we have opa is a last published blog. So this is the first published blog. So right now, just simply make sure that, make sure that the blog that was published last ps first, I want you see minus published dates. We scopy this publish time doesn't what the field publish . speaker 2: time competes. speaker 1: return our string function. Now we also imasay x depth on the score score, xtr on the score score. And I'll PaaS on the self. Now save its own self to tiyou see, yes, if got is already, so wasn't next. So right now, once, once, once to customize the blog save method, what do I mean? So whenever we save the blog, want to capture the value for, want to simply provide the value for the slug and want to also provide the value for the published sh time. Whenever the blog is saved, you understand the value for the slog coming. It got it from the title. And the value for the published time coming gotten from this field is drafts. Whenever I, this field is converted to force, then by default is novice is converted to force. So not capture that time which was converted to force. The stfive is converted to force, which the blog has been published in the stand. So we are going to customize our save method to undo these two cases for the slog and the published sh time we will conduct next. It's time for us to customize the blog, save metal. So pthe cool years makes sense faster. And I still what's going on? So we have the save method. What is it doing? First and fast, I'm going, let's impose lofy. So I'm going just save from from jungle that would ves the texts, that utives the texts. Import log fy, log G5. Yep. So we've got to a log fy imported now. So what we doing now, first and five, we want to simply provide the value for our slog based on the blotype two. Want to get the value for the slog from the blotype two. So I'm going to show you what the slog looks like. I really hope you know what the slog looks like. So I'm gonna to just search for again you see medium and let's go to medium medium dot com now let's click on start . speaker 2: within ok Dev . speaker 1: to channel ads okay now because we have some links I'm going to click on this click on this link right now you can see we have this you can see the title of this this article the title two is how I built the world's best ngx AI scheduling up here is the block title right? If you come a via you can see you can see a via this is the slog the slog was generated from the blostatyou understand the slog in the so we've got so free to assremember we have a list of we have a list of articles here right a list of articles free to asseach of these articles we see the article slog. And the article slog is gotten from the article type two, understand? So if I click on this now, can see up here, we have nine interesting, the type two is called nine interesting open source projects. The log is also called nine interesting open source projects. And we check closely, this slog is, we are all in small letters, and they are also subfitted by an iphone. So that's what it means for it to be lugified. The titwo was lugified and so it's now converted to small letters as and they were separated by iphes in so that's that what is uin there? Right? So what is slog is now so its slog is going from your so that we're doing so we are getting the we we also get our blotitle two. We got seafter getting the titwo. We also to notify in sense solified is making it making it to being smaller task. And each word is subated by an iphone e. After doing that, we also simply get a value and PaaS the value to this variable called base slog. Base slog, right? So we then, then we then we have to create another variable called slog and this, this another variable called slog. And this slog is simply audiis. Simply pointing is simply pointing down to base slog often is up. So that we do now. So right now, once mentioned that our slog is unique, if we check loosely now this slog, as in thus the random character appended to it, this random character is what ensures that the slog is unique because we are going to be getting each of our blog posts by their slog. And to get them, every blog post must have unique slog. So section of our slog is unique, right? That does the aim does aim, right? So you go about that, we also declare a variable. We also we also declare a variable called number. And this number holds the value of one. So over here we have this loop. So right now we are going to simply look through all of our blogs and check to see if any of the blog log already exists. Instead, we are going to check before we save a new blog, we are going to check all the blogs we have and see if the new blog we are about to save, if the slope of that new blog already exists. Now these are is we are going to check. We are going to check for it. So we are going to check for it. So if this if this condition is this loop will keep running and so keep checking if there so right now, if the new block g we are about to create, either the slog of the new blog we are about to create already exists. This this loop is going to keep on running. So keep on running. See, it's going to keep on running. Right? So when does this stop? So right now, if the blog, if the slug of the blog we want to create already exists, what are we going to do? You're are going to simply append this number to the slog to make it unique. So what we did, we have to append this numbers to slog to make it unique. So after doing that, we are then going to increase this number by one. We increase it by one, and instead we also increase it by one so that we don't also keep on appending only this figure one to this today to our slog. Hope you understand. So why this condition is. So if we find any slug that already, if we find that that the new blog we are about to create, any this slog already exists, going to keep on, we're going to keep on increasing the value of number. Understand? So we're going to, we're going to keep on increasing the value of number so we can append it to the slog. So make it unique, I will appear, to what I'm trying to say. So once we are done this command, we are then going to say, the current blog are going to say the slog should be equal to this slog. This time we are going to do so. This command, this this function is it enables us to get our slog from the type two, from the slogified block type two. But if this slogified block types already exists, then we have to conduct this y loop to append int this number to this slog to make it unique. Hope you understand. So that's that. So that's it now. So what next? We also talked about this published time. So how do we capture the value as this, let's call it, publish data that is more friendly or publish time, and it should be published sh dates, not time. Yeah so for the published for the published data I said that by default this is draft is such, right? So what we are going do is this instance. So right now whenever this is draft is set, a force se that means the blog has been published. I want to capture that time. The blog whereby this is draft is set to forth once capture that time, once capture that time, right? So, so right now everything not happen in the save method because you get, so if you are putting this blog, you are going to save the blog. If you are updthis a draft, you are also, you are also going to save the blog. So everything is upin this save method. So via here, I'm just paste this command pacode. So it's going on here. Simple. So by default is draft is. So right now we are trying to say if its draft is false, if it is force se, that means we've converted the blofrom draft to published. So if it is force se on oxo, if save dot is published, it is known, right? If it is known, if sybus is published, that is empty. If it has no value, right, almost simply sets it published. The current time that this, this stuff, upon this stuff, occurright, I want to swenow, we have to import time zone. Let me import time zone gonna to save from uuse. Import time . speaker 2: zone from Jungo . speaker 1: dot sales. So what I was saying is by default is draft is set. You if if it is a draft, that means that means the blog has not yet it's not yet published, right? So right now, if this draft is set to force, that's to say that the blog is being published and we're also going to check if this published, it is known if it is empty, if it is empty, if it if it has no value, we are then going to simply obdates the value of the public states to the current time this condition, this condition took place, right? So that's that an over air will simply just save the I don't to call the save method on this function. And that's all we need to do. And I hope everything works fine. So right now I have coming into our blog model. What next let's go work on the civilizers okay so right now before we to our civilizers let's go work on this on this so we call this featured image let's go configure our let's go let's go configure our media media routes what's the pfile Yeah in this static url just beneath we are going to add a media url we see that should be equal to say img. Right img four slash realso other our media routes media on the score routes should be ecred so going to see the base directory slash but just see media so this all all our images all the images we are going always to be stored in this media folso we are not done yet so so are going go so projects you are l those spfile Yeah we're gonna just say url patterns gonna to say url patterns plus equal so going to say static and then we'll say settings . speaker 2: dots media . speaker 1: underthe score url I'm going to then say documents docuon the score . speaker 2: roots should . speaker 1: be equal to set ings dots media on the . speaker 2: score . speaker 1: on the score rotes so we have to import static on settings so we are going to save from from Jango docom . speaker 2: f . speaker 1: import settings . speaker 2: and I'm gonna . speaker 1: say less import only to say from jungle dot com. Url, the static . speaker 2: import . speaker 1: static. What is happening in Staso? We've got those two imported noalready go to our media url confifigure, so never we update our images, we are going have a new folder called media create set. And this folder is going to be one housing the files we upload. Opunderstand understand? So that it now so next let's create ates, our civilizers for the blog. speaker 2: So here's . speaker 1: our blog. So these are the fields so to create it a blog okay okay okay okay okay so right now to create a blog so I'm gonna to list out I think we need all the fields we need everything the types the we need all the fields right so over here say block block say class block civiliser also civiviliser domodeder civivilizer and I will say class smeser. I PaaS in the . speaker 2: model should . speaker 1: be equal to blog, right? So to save time out, let's import blog and save from from domodels from the models of by file. Import blog. So now passing this fields, I'm going to just paste in this a save time. So we have all the fields from our, we have all the fields from we have all the fields from where from the blog model. So got to our civilizer done. So what's the next step? So I'm going to create a view right away. Let make close move of this guy y's close close. But let's get . speaker 2: a view for that . speaker 1: scennow. Let me close this and let's . speaker 2: say . speaker 1: let's say the . speaker 2: creates blog. speaker 1: right? Second of the requests. So what we're going to do is simple. We're going to denthe civilizer. Civilizer should be equal to block civilizer, to block civilizer. speaker 2: So we . speaker 1: have to import this guy. Let's import it imported from blog up to civilizers. So over since this file, since the model by, since the Visby and this and and and the single set dot five file are booin the same app, there is no need for this block app. You can just save from dots since in same folder, since any same app, right? There is No This for that block up I just removed. So now we've got a civiliquite we have imported about civilized. We've imported it, right? So since we are creating a civilized, we are going to passing these fields. So it's going to be an object. We are going to passing an objects and are are going to just say data should be equal to the objects object and the object is called ore requests the data. And then we are going . speaker 2: to check if this, we are . speaker 1: going to check if the civilizer is valwe are going to check if this civializer is valid, right? I'm going to say, if the zer dot is valid, you're going to then say, civimlize don't save. They are going to return your response, return response on says seriser dot data. And then is an error, we just copy the same error message by three doremember. I said before that you can only create it a blog if you logged in instead. So you can only quthe bloif you are logged in so we are going to add so you have to make sure this you have to add this api decorates so I'm going see adapi underthe score . speaker 2: view . speaker 1: so only as set a post request to create a blog you have to PaaS you have to say you have to make a post request right so last we have to we have to oload another permission so we we have to add another decorator code permission classes write Microsoft to import soon from from rest on the scope framework dopermissions runner imports is authenticated so now we're are going to simply add this new decorates going adpermission classes. So when I say is authenticate that means only logged in users can create blogs, right? Only logged in users can create blog and what more thing is this? Now remember our blog as the blog as the auto field and the auis simply your owner the basically simply the person that rotes this blog you get. So how do we capture this auto? How do we capture is simple. So right now I said that only lothe ui can access this can Crete blogs, right? So right, you can see user should be equal to requests equals douser. This is the logged in user, right? So far, when says right now why we are saving the civilizer, we are going to see auto. This auto right here should be should be equal to this user, the logged in user. So that's it. And once you done now let's go create a new url mapping for this view. So why that should be in the app, you already spfying your app, not in your projects in your junk block app. So going to create a new url path for the view. You'll see parts . speaker 2: scocrates blog. speaker 1: And the the name of devviews is called great crates blog. And let's keep the name create blogs. Everything is the same. So since it is a post, request your advice to add default slash in . speaker 2: France. speaker 1: So that's fine. So right now we've created the url mapping for the blog, for blog for the blog views. So right now, let's go register this blogin in admindows five y file adminthose spy. So why will just come away and say class bloadmin, right? So I'm going I'm going say admin dots model admin and laup right now going to use at least the split list on the scdisplay should be equal ated to right on the show, the underdisplay, the types of the blog for the title. So you want to display the pse is drafts. What else? Let's display category of this blog, the blogs category. And let's also add the created ads of the blog. Created ads. Let's think those four are fine. Let's let's see if we can add any more. Let's let's . speaker 2: see the . speaker 1: blog model and sinthose far are fine. So right now the app, so let's import blog. You say imports from the models file. So right now via just say admin dot sites, dot register, I'll PaaS on the blog and the blog. So this done shows the next step done to refresh. Come on, come on, but save is not down. Let's run the savey again. Grand a. So surviis Bucky game . speaker 2: refresh. speaker 1: So because we have the blogs now, we have the blopeople showing out in the jangle. Admin, Oh, we've no made migrations. Let's make migrations. I'll migrate the blog. Make migrations. speaker 2: Let's . speaker 1: migrate now. Right let's from the sai again okay so refresh this page Yep so we can upate our blogs so remember the url is don't forget the url make close do you have a scored create blog right create blog and if I you conyour blog it must be lobeing right so before we proceed for as for the simple jw let's go back to it so on the left now click on looking for so click on the left panel click on settings so that's wants to that since she wants to configure she wants to simply increase the lifetime of our token because by default our toonly lasts for five minutes and five minutes is just too short it's too small so just copy this object we just copy this copy this. What to your settings of by file? To the bottom of the page, paste this. This should be there, just say 30 minutes, unless the import less go. Let's close the objects. Let's are the closing raor. We have to import time delta, copy this command. And then to the top of your page, the top of your, to the top here. Yeah, so important time dellet's run the servey again. So now it's time for us to create our blog. And if you're not logged in, can't you can't switch the blog because of this decorator, right? Because decorator online sc, if you're are not logged in, you can't, right? So do we log in and then create our web blog? So back to your api. So now by now our access to kill has expired. I tell you that it only lasts for five minutes. We have to create a new access to. So I'm going to create a new access tokey. Click on the send. Click on send. speaker 2: Click on it, waste time. Okay, now we've got . speaker 1: another astalking again. Got a new one. Swenow want to quit our blog, right? So I'm going come over here. So create a blog, the ualways code, create ates blog, the endpoints creates on the scope log. Alright, so and over here, remember I have click on body and then click on form data. So now our bloneeds to title two tisome monlet's say jungle rests for beginners . speaker 2: our blog . speaker 1: music the contents, right? So I'm going to put this same thing as the contents our blog needs the slog. Remember this Sluis is our slog is being generates it automatically whenever we save our slog is being generated it right? So let's leave the slog our blog list see what the blog list see what our blog needs let me put my my cl Lamy the block also needs the okay we don't even we don't need this draft is our our blog list category featured image list add them as category. speaker 2: Remember your category . speaker 1: must be one of these. This must be me loses guys the category must be one of these one of these five either that technology economy, business, sports or lifestyle. So I'm to go for I'm gonna to go for technology, right? Technology. And the featured . speaker 2: image . speaker 1: copy this. So click on this drop down here click on it choose file so want to import want want want to upload the file an image I'll click on of this my image ges shown of this open so that's it let's think we've gotten all the fielthat we need to that we need the other fields that are being generated automatically. There are fields are being generated automatically. This is drafts is set to by default, right? Okay, set to by default. So is no a problem leave that. So the other fields are being generated for us automatically. So I'll remove this paswe don't need it. Let's just delete the Yeah so we've got the title two the contents, the category and the image, the featured image, right? So remember we cannot create a blog if you're are not logged in right now. We have our access booking here. So copy this, access todon't, copy the quotes, just the token itself. Don't copy the quotes, just copy the token. Copy and back to, we are back here. So click on edas. Click on edas. So we're going to simply come over and say authorization, authorization. And then we have to paste our talking here before you pste your talking. Your talking needs a prefix. It needs a prefix. So the prefix is called bera. I will show you where the prefix is called bera. Back to your browser. You can see from this c put wc, if you scroll down, we have these odds edtypes. So yes, it's where the bera is coming from. It's called bera as what the three fixes code berror. So back to via code. You remember here, consider of a via code ore, there's the body, the body, the body holds the object jects. You want to PaaS the back end, it holds the block data. The air does is very good and sets your, your is very good. PaaS your talking whatever key wants to PaaS right now to PaaS the authorization token, which is about access token. I tell that and that token requires a three fixed code bera and then space and then pasting the access token. This all you need to do that it about your body now. So click on send. If you click on send, hopefully we have a new, we get a new blog created. So it's a post request, remember? So click on send. Os, Oh, there is no error. I nine says 200. Ok. So let's take this up. Drag it up right now. Can see, we have a new blog created. You can see the title too. You can see the slog, right? You can see the image, everything, the auto or the auto was captured. But all we are having there is just the otos ID. So right now we are going work on that so we can get the autos, objects, the otos name and and smooinformation. So is draft is false. Okay, is draft is false. Let's see, providing temppower is draft is meant to be, let's see what we did there. It's meant to be what the cforce. Okay, okay, I think I was meant to PaaS is okay. No, since I was meant to PaaS his drafts are part of the body. Why did not I was meant to say is drafts on the score should be but since I did not do that, there's no problem. So now got answer to our back end refresh because now have a new blog is jais for secacy. It's working right? You can create a new blog if you are not logged in. If you notified your jwtoken, your access token, you can't create a blog, right? So that's it. Okay, there's a minwe need to fix from this auto field. Now once we on objects, not just the auto ID, right sort, we go about this. So it's simple, but your code, but your civizes, so the field auor. So going going to create a auto serializer. I'm going to say class, going to just say simple also civilizer right now civiliser domodel model civilizer. And remember, the also is pointing disapointing to our user model. We're going to just see classmates all. And we'll see model should be equal to gets user model and the fields you need, you just need just three fields and saying the ID, the username, the first name and last name, you just need this for fields last almost coming in. speaker 2: So back here. speaker 1: back to the plcks of alizer. We're going to then say also should be equal to simple also civilizer. I would say read only should be equal to that means. We cannot say that means we are not meant to provide a value for auto by ourselves. We don't. We are not meant to do that manually. Like we adminiwe are not meant to provide a value for auto by ourselves. What means is this? That we are not means to Pass Auto as a field. We are not meant to say auto and they provide value. No, the auto is scultured from the Log In User. So we are going quit another blog now. So I will just say, say react for beginners. React. Reacts for beginners, right? So don't worry about the slug. Let me just copy this and replace it here. Let's change the image . speaker 2: to something else. speaker 1: And let's say it's these draft should be. These drafts . speaker 2: should . speaker 1: be equal to okay and then think that's all we need. And then let's create click on assame button says 200. Ok. If you take it up now, you can see we have the aunow, the auis now resoning on objects, not just the ID, right? And we have these drafts are set if this draft is it's published date is going be null. Understand? So it published it is always going have value when its draft is false. You know that if it is a draft, that means the block cannot be seen by the public. It's only when it's when. But if a draft is set of force, that's when the blocan be seen by the public. About Jungo admine if refresh this now you can now we have a new blog because it's draft, it's it's to open now we have a slog. We have consumer. Everything works just as everything just works fine. If I set this to if I if I set this is draft to force and save you're gonna have a value for this publidate so save open it again so code rereacts for beginners so now you can now publish it now as a value right so everything works fine it's moving well so we are done with creating our blog so what less let's go work on lists announthis blog. Okay guys right now we finished, we just worked on creating our blogs in it stanright by before you can create a blog you must be logged in, right? So right now it's time for us to work on listing out this blog post in stanso. Let's want to list them out on want to see the blocks that has been created you so okay, and over over here you can see we have the on this page now we have, we have the list of blocks. We have them that starright now once work on liout this blog post. So we're going to create a new function and I just simply say deaf, I'm going to say block list block on the sclist sts and they're going to have the person the request ests and right now we're going to simply at least not all our blogs. So say blog should be equal to blogs dot objects, but objects that's all right. So let's import nog that be from that should be coming from our models of pi file. Hope I'll just . speaker 2: see from . speaker 1: okay, I know what . speaker 2: to do. Okay. speaker 1: let's just okay, let's see from dosmall delimports blog. Yeah so we've got that imported and we're gonna say blogs objects. We're gonna list out the old blogs and then we say civilizer should be equal als block civilizer. And then we are going to PaaS in those blocks inside. We're going to PaaS in the blogs. We are going to say many should be equal to. That's because we are likely to have more than one blog in. So many should be equal to and they are going to return a response, say with some response and we're going to say civilizer dot data, right? So we've got this already. So what do we do next? Let's go the url for the view. So why here? I'll just copcopy that function name then le . speaker 2: varise. speaker 1: Now I'll say views doblog lists and the name should be equal to views of blog list, right? speaker 2: So so we've got . speaker 1: we've got this already. So we blog so after we've done us now we have to make this a rest function by adding this decorator so paste it here and this this is going be is going and this view is only going to support the gets request ests. So Yeah we've got that already and let's hope it works without any error. So I have to just going upon my flash run server. So see piyson by manage your . speaker 2: spy . speaker 1: run server. Okay, our server is open and running. So as usual we can go to postman, right? So we don't have to use postman for this because there is no don't because right now we are not trying to create a new blog reaction. We are trying to list out the blogs we have because simply is your browser go down to your browser, you just go down once or seven, put 8000 and then you see blog on the score list its answer and right here we have all our blogs and currently we only have two blogs now our database right so we've able to list out the blogs again so so now we've done so right now we have done with list now the blogs so what's next? Let's walk next. Let's walk next on obbit in a specific blog on obbit in a particular blog we're gonna work on that next okay, so guys it's time for us to work on obbin a specific blog. So once obbits blog right? So and this are it work safe we can update the blog he must be logged in and also can only update his own blog. You can't go ahead. You can't update someone else's blog, you can't only update the blog heroes by yourself, right? So right now let's click the function and say Dev and say big updates on the score blog. And then we are going . speaker 2: to have the request. So we've . speaker 1: got the radios. The next thing, so this is going to be, I'm going to copy these two decorators. So first of all, we have the api v decorator and we also have the permission classes. That's the right narrational. So that this, this, this view, it is a protected view. You have to be logged in before you can access the view. Wer, right? So right now I'm going to get the logged in user now. So user should be equal to request douser, right? So we are trying to update the paslab blog right? And right. I'm going to PaaS the blog's primary key. I'll will say pk and via. I say blog should be equal to the blog. That's objects that gets ID equal to pk. But I've turned out already, so also is done. What's the next step? So we're going to check now if blodot auto, if the blogs auto is not equal to the logged in ua, they are going to simply a response, going to save its own. We on response . speaker 2: right . speaker 1: now see arrow. I'm going to . speaker 2: say you are not . speaker 1: the author of this blog. You are not the also. Of this blog going to enter a status, say, going enter status code and that's not 403 going to say, statshould be equal to status. Dohttp on a score, four, three on a score forbidding. So what is all happening now? If I can update the Bloyou must be the auto. You can just go ahead to update someone el's blog, right? So if this condition fails, it is, it is condition is. If this condition is, we are going to understand this response in a standbut. If it is not so, you're going to then say serializer. Should be equal to civiliser, should be equal to block sevliser. Then we are going to simply PaaS on the blog once or update on so business blog, right? So PaaS it in, right? And then I'm going to PaaS, I'm going to say data should be equal to request ests that data, remember I, that this request of data is an object and it is an object containing what these are the fields, these fields, right? This fees and their values, right? So that's what that request of data is. So then I'm going, see, I'm going to check if you're going to check if the civilizer is valid. I'm going to say, if civilizer tois valid, right? So we are going to then say, serialithat don't save. I see this is now Valand. Then I'm going to return your response. We'll say return response. Return response and say civiliative data or if there is any problem, you're going to return on a different response. You can just copy that response from here to save time, copy this response and paste here. So now we've got that already. That's fine. So that we're going to do next left to go, we have to simply liquthe url for . speaker 2: this view. speaker 1: So going to open the url file. Let's open onto to the side. Yeah see now. So I'm going to record this. We'll simply say update blog. speaker 2: right? speaker 1: Now right now, we're going to be pressing the primary key. I'm going to just see int and I'll will say pk, right? And then I'm just simply PaaS. I'm going to say views. That's . speaker 2: peace. That's . speaker 1: what's or bit blog or bit consciously. Then the common variname should be equal . speaker 2: red to v when say . speaker 1: update block, right? So we've got it already. So right now, it's time for also test days to see if it is working. So right now, let's see ala, we've gone, let's try and let's try and test this to see if it's working. So as usual are forfirst shelto get logged in. So that's used for example, but let's go downslow. So our data ase right away going to go answer my to the back end, say one, two, seven, no b pots. So good answer. The admin can access, can login. So now right now only have the blogs, right? So I'm going so for yes, for the first for the first blog which is called vas for beginners can see right? The auto is Daniel. Daniel is the auor, right? So what I'm going do now is I'm going simply, okay, I'm going simply login as Clint and I try obdating this blog that was written by Daniel. So we are going to try that right now. We're going to log and we're log ging as cleanans now. So you know how we do it, right? So we're going to go down to postman. So we're going to open a new request. So let's go on. speaker 2: go on, come on. speaker 1: So it logs log game. We have to go down to say, one to seven. So we are going to log in. We have to come down to the token endpoints, to the toendpoints, and then click on body. You click on form data. And once you've done that, now have to impuyour username and password. The username is called then username is okay, right now I'm I am going to log in as clinusername is called and the password is the password is the password is one so three, four. Thus for cleso you are trying to orbit dance article. So right now, obviously once done, does now make make sure it is a post request and then click the send button. Click click the send button so let's get once get our access key and so now you get our access key request es currently sending Yep so it says 200 ok so now our access key perfect so with got access key so now those next steps so I'm going to open a new a new requests so right now we want to update dates the blog and right now clinston is trying to update someone else's blog right so why you're gonna say one to seven so come back here again back to our code I remember something this is meant to be a put request not posts and if I are trying to update something otmeans she uses a purequest not post post is meant for creating something new what puis meant for updating something that already exists right second just c puts right to that statso I forgot that's very important so right now about the url for this view is currently on the code again it's so right now the url is called update dates it's called update dates blog and you are meant to PaaS on the primary key of the blog you meant to PaaS in the blogs primary key right and remember whenever you are trying to make a post or a purequest it's always advice you put in the the triiling faslayou also put in this last faslashes all this importance so right now we know right now we know we've got our endpoints so back to our code so back to this tab Yep so what are we doing? So I'm going to say one is seven says code updates blog updates. On the scope . speaker 2: blog are going to PaaS . speaker 1: they are going to PaaS on the blogs primary key. So via the primary key of this, the primary key of this blog is two. From what we have open the url, it is two. So back to the box, back to back to vs score, we're going to PaaS into fourth. Slash is meant to be a put request. So once to update White now want to update the blogs, the blocks tie two and oxthe body and also the contents, right? Let's update the type two on the contso. It's simple. Back here, click on body, click on form data. And then I'm going to just say type two should be where you see we . speaker 2: act master class . speaker 1: reacts master class. And the body is going to say, the will simply say, I love rejs. So remember I told you that you will try submitting this now it's gonna to ask for it's going to ask for was christnow asked for the for the for a logged in user so right now going to try submitting it like that let's see send so you can see now it unauthorized you are meant to be logged in you can see action for your authentication credentials says that they are not provided ders less provided right now we are trying to provide the wrong credentials. Remember Daniel is this blog and currently we are logged in as cleansing. So I copied these guy this access to came copies back here you see me just say click click on edand then will see authorization authorization and we're going to PaaS sing the toover. Remember we're also passing the mirror and then space and then pasing your assets token. So we've got that already. See if I try right now I'm trying to lodo this blog post using the wrong uso. Click on send wenice. Wenice says 43 for bidding. You screw up. He says, we are not the author of this block, right? So cleansing is not the auor of this blowe are going to log in as di right away. So via a command puts in Daniel, think it's done and the password is pka on. And then click on send dy. So right now we got, now we've gotten the right access key, the access stocking, the right one copied and then back this tab. So far, the edas we have replace what we have here. We have to use the . speaker 2: right access . speaker 1: key and then we've got that already. And now reference or beat this block consumer. Now the title is saying react for beginners and the body is also saying we act for beginners. Right now we're are trying to update dated to what we have here, right? So let's try that now click on send and let's see. Oh, it says 400ID bad requests. It says the contents field is required or sorry, contents I suppose it strongly it's meant to be a small c. It's meant to be a small c right? So that was a mistake I made. Let's let's cross check this again. So be sure. So okay, the tasis a quiet ad hotels, the contents and all of the things are all automatic automated right? So Yeah so now let's try again so it's meant to be a small c if we check over wire from the models to the small c from the civilizers to the small c right so back here right now say it's 200 roid ok rights so now we have over you can see we have the we have the tiupdated we also have verse the body we have the body updated so box or Jango admin very fresh speech now we are gonna see we act master class and over you're gonna see I love react Jas so let's just refresh casinnow it has worked it has it has been upated now so it's working fine so what's next now so next we are going to work on is delete team we're going to show you how to to delete this particular blog so we are working on that next okay guys right now we just finished working on obbeating a particular blog so what next let's work on deleting the blog it's very easy box our views back here again just come over over here or copy this this to the curators copy them and if once copied now we are going to create a new function going to say delete blog click on the scope blog so this is going to be . speaker 2: a post request ests . speaker 1: then I'm going to PaaS on . speaker 2: the requests and the blog. speaker 1: Speak right, the primary key. So we're are going to get the blog going say blog should be equal to blog should be equal to blothat's objects dogets ID equal to pk and then we're going to simply say, I'm going to check the log recommento make that the user lithe blog is the owner of the blog. I'm going to see that because request that's user and check if to see want to check if blog dot also it's not equal to the user, the ability to return a response that says, let me just copy the same response . speaker 2: we have here. speaker 1: the same thing, a ctrz cc copy, then p starts here. So you can only, you can only delete the blog erwrote. You can't come up here and delete someone else's block g you get so so if the blog auto is not dellogged in useven on those response, else we are going to delete the blog. Let's say blog dot deletes, right? And then we're going to return on a response. Let's see with on right now, we're going to return on response, going to save which own response. And now just put somebody the let me just say I couldn't return the message that says just a blog delete says successfully and they are going to come status that is equal to status equal to status der ecp underscore two o four no contents. Right. So that's it. So because bloview done so as next nice quick let's create a view a url for this for this blog for this for let's the url for this delete blog function so far PaaS on the parts, then going to PaaS on the primary key. And remember, we also passing the tralling slash there. I will simply just say views, dots, delete blog and the name should be equal to the ete blog oso. Yes. So we've got this already. We've got it set. So it's simple. We are going to try the same thing again. I'm going to just quickly in so quickly create a new blog. Let's just create a new blog. Now come by and just create blog. Create ates an scope blog. So I just I just really hope that for code, our Adour, the access key still vid less of so so with a new blog or . speaker 2: will say next . speaker 1: year has 15 crash course. The content should be, say, I love coding as a contents. As for the, as for the featured image place, add an image assucreate blog that we can delete, then I'll select it file here. Just with random image there. Okay, that's fine. Just say is drafts should be equal to mostly these drafts on the scodraft should be equal to force. So let's send so . speaker 2: right now just add a new block g . speaker 1: created set. That's fine. Now right now once to delete this block g once to delete the blog and it's only done that can delete this block only done can do that cleansing can come. Why White devices? So that's because done is the owner of the blog instead of cleansing cannot come away to delete dance blog. It's not possible. So we tried right? I'm going . speaker 2: to just say delete . speaker 1: g deletes blog. We're going to PaaS on the primary key. Past that here, I'm going to try logging in as cleansing. Now back here I'll just say cleans and the pusword is one to 34. So we got to our access key. This is Clinton's access key. So Clinton is trying to delete dance block, and that is not going to work because it's not the owner, right? So so please access kivf there. Paste this one. So back to the body file, click on send. Turning is right in right, everything on the arrow, he says saver error. I think it's because I forgot to add default last year, but instead lesbut. I think that is one of the reasons. But check, but let me just check my code to see what the error message is. Let's see. Click on the save again run Sava. So I think it's because I did not add this poslash behind. So our url is called deletes block and then passing the primary key switshould work are going to try it again. We'll try it some more time. So let's see if our saver is up again. Okay, French run saver is taking time. Why? So surviis op game right? So back here the person puts in the fourth slash there and then. Okay, what are we going to do now? We're are to crso now we are trying to delete this blog. We are trying to delete it, right? We don't even need to PaaS. We don't even need to PaaS any of these. We don't need to PaaS any of these, any of these fields because we are trying to delete. I just realithat now I'll will clear them off. You don't need to PaaS any of them so click on send White 94White nine and arrow says you know the odds of this blog. That's because you are currently logging as cleansing and Daniel is the owner of the blog so only Daniel can delete this blog in the stand. So that's that's what we have in this error message. So we are good now. Okay guys, right now it's time for us to update a user's profile, right? So I'm gonna to show you what I'm talking about. Remember back here in our civizer, by placing user, we only provide values for the email, the username, the first name, last name and password. And remember closely we had to customize our user model. We added the extra fields like bio, the provide file picture of the Facebook url we had to understand on the YouTube, Instagram and tweeter, right? So these other fields don't have any value in a same because why christnet user? We only provide values for the most important fields. So right now, once give users the ability to update their profile and provide values for this other fielso, we are going to right now we are going to be working on on the user's profile updates, right? So we're going to create a new civilizer in your civilizer, right? So what I'm going to do is I'm going to simply copy this one. It's going to be similar to this one. I just specithis guy. Yeah, Yeah. I'm going to call this user or Buser profile, let's say user profile update civilizer, let's go update dates. User professional, let's go updates. User profile civilizer, right? So I'm going add in the extra fields like the bio. I'm going, I'm going to remove the password. We don't need to update the password. I'm going just say the bio, right? Profile also the one because it's got maybe we show, I think it's got profile picture, put file picture and then the social media links, right. Okay, let me just close this guy upon us to the right. Right says our . speaker 2: civilizer . speaker 1: says got proof our picture before far on . speaker 2: the school picture . speaker 1: now are the other ones. Facebook, we have each shape. We have Instagram and we also soft Twitter, which is known as x currently. So yes, I've added the other huge ones of update. So we've got the feed here. So raps are perfect, right? So what's the next phase? So we're going to create it, a view for the civilizer. And just here, quick ative view just under the under the register user. Let me just make them own mestogether or creative features just under the register user serial. It must not be there, but I want to just make my day. So right now, I'm going to PaaS the api, view the api with the curorito, and it's not accepts the puts request because you're trying to date an existing record. And we are going to also add this permission class because it's only log the user that can or beither a profile, sardev updates on the score user profile, uon the score profile when passing the requests. speaker 2: We're going to get it logged . speaker 1: in user going. This user should be equal to request. Let's user request. That user is so simple. And then what are going to do next? I'm going to say civializer should may equal . speaker 2: to updates. speaker 1: Update user profile. speaker 2: Civiliser going to PaaS . speaker 1: in the user. We are going to PaaS in this user because because if this user wants updates, they are going to PaaS in the requests objects. When I say data should be equal to requests, dots objects, my correct equof data, not such object. Sorry, equof data. They are going to simply import this this class also down control on the period sign of your keyboard. Yeah. So you already got it imported area with it. Right? So back here I'm going to check if the civilizer is valid. Going to say if the civilizer is valid. Then we are going to simply save when says women says civiliwe are going say the civiliare going to say civiliser don't save, right? So we've got that there. And then we're going to return a response from same result response. They will say civiser dot data else you're going to else you going to return this error response, right? Just copy this and paste it. Yeah. So Yeah, so we've got already so we've got wow this is a profile set so we're going to we're going to create for going to create ates a url for the view and just here your . speaker 2: in the parts . speaker 1: so Yeah we're going to say a bit una score user then I want to PaaS on the views they're going to say views dots updates user profile and the name should be equal to deletes blog . speaker 2: sorry she . speaker 1: may got to update dates you sir Yeah so we've got this already so let's try it out let's try it out right away okay so yeso let's try it however I try it out now go down let's go down so add mindows five file add mindows five so like to add right now good on user if good on stijungo admin check out our users . speaker 2: come on come on . speaker 1: come on my server is now down let me run the seragain. As server ouso . speaker 2: you have an . speaker 1: arrow and that's coming from the ur elder spy file. So I think we've where are the comma? We've got what the comma here. This comma is important. So so are we. Our software is going to reload by itself. So why is this reload in? Okay, let's wait for it to reload because I want to show you something. So now back to the browser. speaker 2: Let's refresh this. speaker 1: So now from what we have here now this is the user table. We only have these three fields, the username, the email address, the first name, the last name, right? Then I want to be able to add the bio. I want to add other fields. So I want able to I want also see the other fields we have here like the bio, the profile picture and the rest. Right? So back here. So back to our list display. I'm gonna add I'm going to remove this creates okay, I'm going to simply remove we are not using the email for anything. Let's remove email. I'm not working with the email. So I'm going just add let's say the bio, the profile picture, the Facebook, Instagram. We have Twitter . speaker 2: hotels . speaker 1: else again, okay, we only have tweeter a on YouTube, right? Let's add the shape. Let me just click this. Yeah. So we've got this now. So come back here, refresh the page. So you can see now we have everything showing there that's perfect maright now you can see we have all the fields now or showing. You can see the other ones are blank because we don't have values. We don't have values for them yet. So you can see the bio is empty for both users. Let me just I also wish I can collapse this. Okay, let's see. Okay, Yeah. So perfect now so can only have users, the buyer is blank for both. They don't, they don't put on up five pictures. So they qutheir account only. They only provided value for their username, the first name and the last names right now also update ones to give them the ability for them to update their profiles and provide values for the other fields, right? So now it's simple. We will be doing this before subbox for view again. You can see if you can do this, you must be logged in. We can only update the logged in user's profile. So here's why. Url discord update dates, user subback to postman. So it now also updates series update cleansense plus updates clean sense se record. So I'm going to simply send its own screen, a new access to king because the one we have here might have expired. So send Yep. So we've got a new access token, right? So let me just reduce the size of this thing. So we've got we've got a new access token, right? Let's update the profile. So back here gonna just say update user updates on the score user. speaker 2: Okay. speaker 1: so now user rights. I'm going to copy that stocking recorhere copy the access token back here. I'm going to go down to the edas secondary episode we have with this. Trying to update the user. I'm trying to update clean sense profile swabody a sounds to simply add a bia biography. And I say I am a full sti, am a full, I am a full web developer . speaker 2: and coding . speaker 1: instructor, right? So this is links, bio and coding instructor. Sounds also giving my a profile picture profile on a scoppicture. Okay, just speaon this because we have, we have a drop down here. Click slate text, select file one. Ops, I'm going to give clinname for file picture. I just hope I find something I can use. You can have one here. Oppposounds also gave clinscene or because is let me just add a Facebook url for Clinton Facebook and I'll sample you have oem just copy this guy. Copy this must . speaker 2: be a url. Because as . speaker 1: this must be a url, let me just add one more. You let me just add the youshape for cleansing youshape. I think I have each supervisor. So just copy this. Let me just close on the space. So I think I've got the YouTube url how paste it here. So Clinton only asked see right now Clinton only asked to social media Facebook and YouTube. So right now I've given him a bio perfect five picture, right? So we hope this works. Let's hope it works. Now we're gonna send we can also decide to update Clinton' S Clinton's first name or his ui can just see his, you know this name is called Clinton. It's called cleright sounds right now I want to change to cleansing or let's call it code with cleansing. I'll just say code with cleansing. That's going to be the username, right? So now I'm trying to update, I'm also trying to update the username we have here, right? So let's open this works. So let's hope we're not going to fix any error. We are going to not be a boots request. Remember, let's see if we did that. It should be a, it should be a puts request puts, right? So yes, back here, choose puts and then send, let's send. Perfect as this is 200. Ok, right? So now it's worked. It's worked like the cleansing. Now as a bio, you know, as a perfect picture, you can see we change this sername from cleansing to code with cleansing. So back. So our browser. Now if I refresh this page, refresh casino ksiner as cacleansina as his name has been changed from clinse to Covid cleansing. Now as a bio, now as a profile picture, it has a Facebook url. If I open the profile picture now, let's see what it looks like because this is clinthis profile picture, right? It's working, isn't it? It's working able to successfully or beats a user, a user's profile. So Clinton, now as Facebook, you scroll, it also has YouTube and Facebook now. So I'll showed you how you can update a user's proffile. So right now we are going to deploy this application on renda dot com. So we deploy the backend on the front taon renda dot com. So so right now, let's deploy our application. So first and first we are going to start with the backend. So we coming first to the back end first, understand? So yes, what was the first we need to do? So what do we need first now? So we need to install a package called gunicon. So gunicon is used to run our saver in production instead. So gunicon epepdes run our saver in production you get so make sure you make sure you have your virtual environment ments activated and then run this command. Keep install ed gunicon, sorry I didn't run that peep install make this bigger. Okay. Peep install . speaker 2: gunicon . speaker 1: so each answer so right now when icon has been installed, okay, okay, already have gon installed you can see says the requirement already satisfied. Okay, that's fine. So what's the next step? Was next, we Manato do. So we need to simply, we need to simply quit our file code. We, we, we need to simply add gnion to this file called requirement of cst. So if you don't have this far, don't worry, just simply run this command, peep freeze, peep freeze, then this greater than sign, the greater than sign. So then to say peep freeze, then then the sign, and then save requirements dot cxt. So this file contains all the packages that is required to run our application. How this file contains, right? So just hit enter. So right now I'm trying to add gunicon to my already made list sts understand which you so as for you, if you don't have the file, you can simply just run pip three to generate the file that it so now we have going on installed ready up the requirements file ready. So the next step. So right, we need to go configure our our static files, right? So go to your go over to your settings of by file. So just under the static url over you're going to just say static on the score root should be equal to we say base directory slash static make sure to score static because render watches for the static folder. So make sure to scstatic. Right? So we've done that now. So you see you need go over to your ul dopi file in your jungle project. So opponents, your udot pfile. So why we did this for the media, for the media url and for the media routhis time you see are we are going to duplicate sets for the static routes. So install the media urwe are going to have the static url, the importance, and this disis going . speaker 2: to be the static roright. speaker 1: the static rotes. So Yeah, that is one step unbupped to your settings of pfile over to your allowed hosts. So simply just add this astamic here. So I want to accept whichever Howe have there. So just add this line there, this astamic in quotes. So that's it. So okay, box here. So box cks are called now. So in the root in in your in the root directory, the same place. So you have your your your requirements file, the same way of the manaspi file, your your database. You're going to create a new file. I'm going to call the file build, then build in small tagon to say build dosh, and then I'm going to pain the code here. So Yeah, just paing the code here. This this one helps us to install our requirements. Like it helps us study content of our requirement of csfile. They that were the first commanders. So this one exto run the call. It's abto run the call. Static command. That's that's, that's that's for that's for this for the static files. And the last one exhelps to make exto migrate our database, right? So this command now we for this file is because we are trying to run multiple commands. So the best way to do it is to store the commands in the file, right? So Yeah, that's it. So now the next thing you have to do now is push your code to GitHub. So push your code to GitHub and once you've done that, so proceed. So I'm going to my I'm going to simply update my repository on GitHub. I'll open a new git stamina giit page. Okay, so right now I have my git terminal open someone who simply updates my git repository. So these almost Gare ten dots then also gets commits I just say prepping for deployments. Prepping for deployments as for you guys in mind, so as for you make sure you push your code giup for me I already have my code on githusome trying to update dates the code the code I have on GitHub first for you if you don't have your code on GitHub you have to push your code first before you can't do any of this. But so far for me I'm trying to this my my my already existing code on GitHub right so then I want to say gpush origin main gits push origin main then its answer. So right now, so I just puthe GitHub, so I'll go back to my browser. Let's look up GitHub. Githudot. Com. Where are you? So my giare busy, so I'll simply open, let me check our own right now and check out my repositories. So right now Yeah you see right now this this is the repository, this White cthe folio api opit. So Yeah, we have so I just pushed this I just made some updates and can see in the last two minutes we just pushed our latest updates prepping for deployment. So I think we are good to go over to renda. Go on your browser and look up renda dot com. So if you don't if you don't know if you don't have an account, simply sign up using your GitHub account. I recommend you sign up with your GitHub account. That is fast and quick. It's fast and easy. So once you are logged in, I'll 16% simply click on dashboard on the top right. Okay, so it's loin. Yeah. So right now I'm going to quit a new project. There's no more. Just quit a new project. Okay. You know, right now we are trying to use a free plan, so already have a project code, the full use. If I add a new one, it's it's accand make payment. So I already have already have a project se called the full use that for economies that like that. So economthis project like that. So don't worry about christprojects. It's not more secretive projects. It's not more at all. So once you click on the dashboard, remember we came from here. Remember we just click on dashboard on the top, right, right click on dashboard and we are yet now so sure. Yeah, there it is not a most for tive project. No. So on the Nabar on top here you see we have this new box and click on it then select web service. Web service right? So now we want to pick our Guto repository. We want to pick it. So click on the credentials on the right click on this credentials then select GitHub, configure GitHub, right? So we're going be logged into GitHub already. I'm right now I am logged into my own GitHub account. So as for if you're not logging, it's gonna actually for your passport sword to log into GitHub right? It's going to actually for your GitHub password to log in. So as for me, I already logged in so I'm going pick my account. Yes, could be cleansing. Click on it. So right now, Yeah, now so just scroll down. You're going to see select posisitories. So right now transelection you are not picking all the repositories. So click on this drop down. So you remember my my the name of the repository is called White c on a called defolio. So it's White c on a called defolio on a called api. So here we see, we select this right now consists up here, and then just scroll down and then click on save. So we are being with directed back to render right now we on rada now and we have right now we have our repository on Rader White c on the defolio on the api. So click on it and then scroll down click on connect. So it's loading now. So I just come over and I'll give my, I'm going to give this a unique name. I'll just call it the folio, the folio underthe score. Api starts fine. So it's ato pick a project. So can see this is optional. So I also pick the folio. They pick the but it is optionalized. It's not immense. If you don't pick, it's fine. So as for the build command, the build command is going to be so it's going to be sh build dot sh. So what now we are we referring to the file? We created this build dot sh file for the build command. So we have to for the four pish, then build dot sh. As for the statcommand, that's going to be gincome on the the name m of your jungle project, which is my own, is called jungle project. Mine for me now my jungle project is called new underscjungle underscapi, this is a fothat I omy, 16 ens of pfive. That's the jungle project. The that the folder I owds your 16 of five, five is your jungle projects. So my is called new on scojungle underscapi so back here want to say gunthen new underthe scjungle underscore api and c dowhhgi rice. Then I'm going to say application. So if we recheck loosely, this wi is also in the same location as our settings of pfile. They are both inside of the jungle projects, which is new jungle, new Jungo api, which is new on dascjungo, on scapi in the stand. So that's that. So I think we making progress grenow. So the next step. So now we're going, we're going to, right now we are going to go with a free plan. So remember this remember this is just a tutorial. I also are trying things out so you're going to go with a free plan. So Yeah now click on that and then scroll down and click on deploy web service. So let's wait for me to deploy. It's gonna take it's not take some some few minutes. It's not gonna to take time. So currently tly this building so once it's done building it's gonna deploy it and then we'll see if we are good to go. So you're gonna give this some time to build and then deploy web p up the cloud. Let's wait for it. So right now we still building and currently seen in progress. So it's now deploying so it is almost done almost done with the deployment less weights. So right now you can see says your service is live. So I think we are good to go and hope this and I really hope this does not show any error. Says right now it says your service is live. So Stroback up you can see our url. Yes, let's the back end url just copy it with answer a new tab and then paste it. speaker 2: Paste. speaker 1: Let's see. So now let's go down to slash admin. Let's see our admin. It's answer so . speaker 2: perfectly because I . speaker 1: admin now. So right now I'm trying to check out our see one right now I'm I'm trying I'm trying to see any of our endpoints. Let me just open the blog up. Okay, next go to block g lists. Let's see. It looks like blog list, block on ascalled lists, blog on the score lists. Its answer, perfect. Now can see now with right now we have successfully deployed the back end to the cloud. So yes, the live url. So this buend can be accessed from anywhere in the world.
最新摘要 (详细摘要)
概览/核心摘要 (Executive Summary)
本内容是对一个使用Django和Django REST Framework (DRF) 构建REST API速成课程的全面总结。该教程由主讲人Clinton引导,旨在通过一个博客应用的后端项目,从零开始教授学员如何独立、自信地构建并部署一个功能完备的REST API。
教程的核心亮点在于其系统性和实践性。它始于最基础的环境搭建,包括安装Python包(Django, DRF, Pillow, Gunicorn等)和配置虚拟环境。一个关键的最佳实践被反复强调:项目初期就应自定义用户模型(Custom User Model),以增强未来扩展性。随后,教程详细讲解了如何利用DRF的序列化器(Serializers)、视图(Views)和URL配置,实现用户注册功能,并集成了Simple JWT (JSON Web Token) 进行用户认证,确保API的安全性。
项目主体围绕一个博客应用展开,详细实现了完整的CRUD(创建、读取、更新、删除)操作。特别值得注意的是,教程在权限控制方面做得非常细致,例如,通过明确的逻辑检查确保只有博文的作者本人才能更新或删除自己的文章。此外,还实现了用户个人资料的更新功能。最后,教程以将后端应用部署到云平台Render.com收尾,涵盖了生产环境配置(如Gunicorn、静态文件处理)和部署流程,使学员能够将所学知识应用于真实世界。
项目目标与技术栈
- 最终目标: 构建一个全栈博客应用。
- 当前系列焦点: 专注于后端REST API的开发。
- 后续计划: 在完成API后,将使用React构建前端应用来消费此API。
- 核心技术:
- 后端框架: Django
- API框架: Django REST Framework (DRF)
- 数据库: 默认的SQLite
- 认证: Simple JWT (JSON Web Token)
- 图片处理: Pillow
- 部署: Gunicorn, Render.com
环境搭建与项目初始化
-
基础环境设置:
- 使用命令行工具(Windows为
cmd,Mac为terminal)进行操作。 - 通过安装
virtualenvwrapper-win等工具创建并激活独立的Python虚拟环境,以隔离项目依赖。
- 使用命令行工具(Windows为
-
安装核心依赖:
- 在激活的虚拟环境中,通过
pip安装必要的包:pip install Djangopip install djangorestframeworkpip install djangorestframework-simplejwtpip install pillowpip install gunicorn
- 在激活的虚拟环境中,通过
-
项目与应用创建:
- 创建Django项目:
django-admin startproject new_jungle_api - 创建Django应用:
python manage.py startapp blog_app - 在
settings.py的INSTALLED_APPS中注册新创建的blog_app和rest_framework。
- 创建Django项目:
自定义用户模型 (Best Practice)
- 动机: 默认的Django用户模型字段有限,无法满足未来博客应用的需求,如个人简介(bio)、社交媒体链接、头像(profile picture)等。
- 核心观点: 主讲人强烈建议,在任何Django项目的初期就应该自定义用户模型,以避免项目中后期修改模型的复杂性。
- 实现步骤:
-
在
blog_app/models.py中,创建CustomUser类,继承自AbstractUser。
```python
# blog_app/models.py (伪代码示例)
from django.contrib.auth.models import AbstractUser
from django.db import modelsclass CustomUser(AbstractUser):
bio = models.TextField(blank=True)
profile_picture = models.ImageField(upload_to='profile_pics/', blank=True)
# ... 其他社交链接字段 ...
2. 在`settings.py`中指定新的用户模型:pythonsettings.py
AUTH_USER_MODEL = 'blog_app.CustomUser'
`` 3. 在admin.py中注册CustomUser模型,并在后台管理中展示。 4. 执行数据库迁移:python manage.py makemigrations和python manage.py migrate`。
-
用户注册与JWT认证
-
用户注册 (Sign Up):
- Serializer创建: 创建
UserRegistrationSerializer,包含email,username,password等字段。 - 安全措施:
password字段被设置为write_only=True,确保密码仅用于创建用户,不会在API响应中被返回。 - 重写
.create()方法: 为了正确处理密码,重写了Serializer的create方法,使用user.set_password(password)来对密码进行哈希处理后再保存。 - View创建: 创建一个基于函数的视图
register_user,使用@api_view(['POST'])装饰器。
- Serializer创建: 创建
-
用户登录与认证 (Login & Authentication):
- 集成Simple JWT:
- 在
settings.py中配置REST_FRAMEWORK,将simplejwt的认证类设为默认。
python # settings.py (伪代码示例) REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ) } - 在主
urls.py中,包含simplejwt提供的URL路由,用于获取和刷新token。
- 在
- Token生命周期配置: 在
settings.py中配置SIMPLE_JWT,通过ACCESS_TOKEN_LIFETIME将访问令牌的有效期从默认的5分钟延长至30分钟。
- 集成Simple JWT:
博客API - CRUD功能实现
模型与序列化器
- Blog模型: 创建
Blog模型,包含title,content,category,featured_image等字段,并通过ForeignKey关联到CustomUser模型。 - 关键实现与考量:
- 自动生成Slug: 重写模型的
save()方法,使用slugify根据title自动生成唯一的slug。教程中的代码包含了处理slug命名冲突的逻辑,确保每个slug的唯一性。 - 自动记录发布时间: 在
save()方法中,通过逻辑判断当is_draft字段首次变为False时,自动记录当前的published_time。
- 自动生成Slug: 重写模型的
- Blog序列化器:
- 创建
BlogSerializer,并使用一个嵌套的AuthorSerializer来序列化author字段,使其返回作者的详细信息(如用户名、姓名),而不仅仅是ID。 author字段被设为read_only=True,因为其值将从请求的用户中自动获取,而非由客户端直接提供。
- 创建
视图与权限控制 (Views & Permissions)
-
创建博文 (Create - POST):
- 权限: 使用
@permission_classes([IsAuthenticated])装饰器,强制要求用户必须登录。 - 逻辑: 在保存序列化器时,自动将
author字段设置为当前登录用户 (serializer.save(author=request.user))。
- 权限: 使用
-
读取博文列表 (Read - GET):
- 权限: 无特殊权限,任何人都可以查看博文列表。
-
更新博文 (Update - PUT):
- 权限:
- 用户必须登录 (
IsAuthenticated)。 - 所有权检查: 在视图逻辑中,添加明确检查,确保只有博文的作者才能更新。
python # views.py (伪代码示例) if blog.author != request.user: return Response( {"error": "You are not the author of this blog."}, status=status.HTTP_403_FORBIDDEN )
- 用户必须登录 (
- 权限:
-
删除博文 (Delete - DELETE):
- 权限: 与更新操作相同,必须是已登录的博文作者本人。
用户个人资料更新
- 功能: 允许已登录用户更新其个人资料中的自定义字段(如
bio,profile_picture, 社交链接等)。 - 实现: 创建一个新的
UpdateUserProfileSerializer和一个update_user_profile视图(PUT请求),该视图要求用户必须登录,并直接获取当前登录用户 (request.user) 的实例进行更新。
API测试
- 教程全程使用Postman(VS Code插件版)来测试API的各个端点。
- 关键测试流程:
- 向注册端点发送POST请求创建新用户。
- 向token获取端点发送POST请求,用用户名和密码获取JWT
access和refreshtoken。 - 对于需要认证的端点,在请求的Headers中添加
Authorization字段,其值为Bearer <your_access_token>。 - 演示了权限控制的有效性:尝试用一个用户的token去修改另一个用户的博文,会收到
403 Forbidden错误。
部署到Render.com
生产环境配置
- WSGI服务器: 安装
gunicorn作为生产环境的Web服务器。 - 依赖管理: 通过
pip freeze > requirements.txt生成包含所有项目依赖的文件。 - 静态文件: 在
settings.py中配置STATIC_ROOT,用于collectstatic命令收集所有静态文件到指定目录。 - 允许的主机: 在
settings.py的ALLOWED_HOSTS中添加'*'或Render提供的域名,以允许外部访问。
部署流程
- 部署脚本: 创建一个
build.sh文件,用于在Render服务器上执行一系列自动化命令:pip install -r requirements.txtpython manage.py collectstatic --no-inputpython manage.py migrate
- Render.com配置:
- 将项目代码推送到GitHub仓库。
- 在Render上创建一个新的"Web Service",并连接到对应的GitHub仓库。
- Build Command:
sh build.sh - Start Command:
gunicorn new_jungle_api.wsgi:application - 选择免费套餐并部署。
- 最终结果: 成功将Django REST API部署到云端,并获得一个公开可访问的URL。