Build An API With The Django Rest Framework Using Python | John Elder | Skillshare

Build An API With The Django Rest Framework Using Python

John Elder, Learn To Code!

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
19 Lessons (1h 10m)
    • 1. Introduction

      2:52
    • 2. Install Python

      3:05
    • 3. Install Sublime Text and Git Bash Terminal

      5:03
    • 4. Set Up a Virtual Environment

      3:01
    • 5. Install Django and the Rest Framework

      2:27
    • 6. Start Our Project

      2:51
    • 7. Create an App with Startapp and Create URLS.py

      4:13
    • 8. Django Admin Area

      3:25
    • 9. Create Our Models Class

      5:10
    • 10. Add Our Model To The Admin Area

      4:30
    • 11. Create a Serializer

      4:24
    • 12. Create Views

      3:31
    • 13. Create Urls and Routes

      5:01
    • 14. Switch To Hyperlinked Serializers

      4:31
    • 15. Install Postman

      2:40
    • 16. Use Postman to Confirm Our API Works

      2:36
    • 17. Set Up Version Control With Git

      3:55
    • 18. Push Our Code To Github

      3:33
    • 19. Review The Project

      3:19

About This Class

Building your own API with Django and Python can seem overwhelming at first, but it doesn;t have to be! In this course I’ll walk you through it step by step and you’ll be building your first API in MINUTES. You’ll be amazed how quick and easy it is to create very professional API's using the Django Rest Framework, even if you have no programming or web design experience at all.

Watch over my shoulder as I build a simple little API that lists some of my online courses step by step right in front of you. You’ll follow along and build your own copy. By the time we’re finished, you’ll have a solid understanding of the Django Rest Framework and how to use it to build awesome API's that will serve up any data you like.

The course contains 20 videos – and is just over 1.5 hours long. Watch the videos at your own pace, and post questions along the way if you get stuck. You don’t need any special knowledge or software to take this course, though any experience with basic Django or Python is a plus. You don’t really need to know the Python programming language, but it certainly helps!. I’ll walk you through EVERYTHING.

Django is a great web development tool and learning it has never been this easy.

What We'll Build...

We'll build an API that let's you show any sort of data that you like.  The data will be publicly accessible through your API using JSON, just like any API you've ever used before.

We'll mainly use the popular Django Rest Framework to build and server our App. You'll learn how to create a database model to hold your data, how to build a Serializer to convert the database data to and from JSON, and urls and Routes to create webpages for you API.

Sign up today and I'll see you on the inside!

Transcripts

1. Introduction: All right. Welcome to the course. My name is John Elder from coding me dot com And I'll be your instructor today. And this course we're gonna learn how to build an A p i with Django and Python. And so you could see this is what we're gonna be building out here. It's just a basic AP I that other people can connect to you and grab data from so you could see we've got this website we're gonna build that goes along with it. You can look at it from as Jason. Here's the information. This stuff will be contained in a data base, will learn how to do that. And that should be a lot of fun. So in the next couple of videos, we're gonna jump in and start downloading the different tools that we need. Everything is free. There's nothing more you have to pay. I just wanted to mention before we get started, I'm on a Windows computer. Hopefully you are too. If you're on a Mac or Lennox, you should be able to follow along from time to time. There may be a command or something on the terminal that's slightly different. I'll try and point those out. If there's something I miss because I just start thinking Windows and I forget about Mac and Linux, shoot me a quick message and I'll tell you what you need to dio or you can google it. You know, a lot of times you can quickly find an answer. You know how to do such and such command on a Mac. It'll pop right up, but definitely send me an email if you can't figure it out. Um, that's another thing I want to mention. If there's anything throughout discourse that you don't understand if something doesn't quite work right, if you're getting errors in your code, absolutely feel free to contact me any time. I'm here in Vegas, sort of West Coast Pacific time, so keep that in mind. If you're on the other side of the world and it's during the day, I'm probably sleeping, but as soon as I wake up, I'll try and get your question right away. This is what I do for a living. I create courses and I spend my day answering questions for students, So I'm always around answering questions and I want to really make sure you understand this stuff, so if there's something even you just don't quite understand, if it's just not clicking, shoot me a message, let me know and we'll walk you through it and absolutely happy to do that. That's what you get from the core sick. That's included in the price for sure. Uh, so feel free to do that any time. Also, depending on where you're watching this course, the system may ask you for a review. I would absolutely appreciate if you, you know, shoot out a quick review if you're so inclined or whatever. Even just giving a star review without typing something is good is always useful and beneficial for us. It helps with the rankings and the courses, and any feedback you have is definitely useful. Don't feel feel, feel like you have to, you know, like some places. They ask you for a review right at the beginning of the course, when you haven't yet had timeto really understand. But those places always let you update later on, so if it seems to be good, you leave a good review and later you change your mind. You don't like it. You can always change that back or vice versa. Eso that's kind of cool. So, anyway, I think that's all for the kind of intro stuff. In the next video, we'll jump in and start downloading and installing all the tools that we're gonna need and should be a lot of fun. 2. Install Python: okay, This video, we're gonna jump right in and start downloading and installing python. So if you already have python installed on your computer and there's a chance you already do If this is not your first python thing, I'm still gonna recommend that you uninstall it and reinstall and I'll show you why in just a second. But to uninstall it on windows, just go to your Windows Start menu typing control panel and this will pop up. Come down here to uninstall a program, click on it, just scroll down here until you find Python Double click it to an uninstall it so pretty simple. So to install this thing, let's go to Google and I'm just gonna type in Python. And you see, the first thing here is python dot org's will go there, and this is the official Python website, So we want to go to downloads here. And here's the button for Windows. If you're on macro Lennox, you'll see the Mac at Lenox Button. But notice we're on version 3.7 point four now. If this version number changes by the time you watch this course, no big deal. Just download the latest one, as long as it's not like four and above as long as it's below for you'll be fine. Major version updates are like the first number. So from 3 to 4 from 2 to 3, if it's somewhere in three, you're fine. So go ahead and click here to download, and we can save this anywhere. I'm just going to save it to my desktop, and I'm on Firefox. So my downloads appear up here on this little arrow. If you're on chrome, they appear down here. Wherever it appears on your computers, go ahead and click it when it finishes downloading. It's a pretty small file, so it shouldn't take very long. So we just click this to install it. And here's the installation. Now. This is why I want you to uninstall and reinstall it. See down here, says Add Python 3.72 path. This is unchecked by default, and this is very important. We want to make sure that this is checked so that python gets added to our path. And what that means is on Windows computer, at least that you can run python from anywhere on your computer. You don't have to be in the Python directory in order to run it. If we click this little box, that's so we can run python anywhere but by default. For some reason, it's unchecked and tries me crazy. Why, that is because you always want to add python to pass, so make sure that's checked and then come up here and just click. Install now. And this doesn't take very long, and you'll notice it's this 32 bit. Now I'm on a 64 bit computer. Chances are you are, too. Why are we downloading 32 bit version? Why aren't we installing the 64 bit version? Well, the 32 bit version is the one you want. The 64 bit version is a little different, and there's some things in it that we don't really ever need or use but could cause problems. So just the standard default version is the 32 bit version. That's the correct version you want. That's the one everyone uses. So don't be like, oh, I've got a 64 bit computer. I need the 60 port four bit version of python. No, you want the 32 bit. So Okay, so it was successful and that's it. We're ready to go. It is just that easy to install python on windows and a very cool. So in the next video, we will start to look at and download and install the other tools that we're gonna need the text editor in the terminal and that'll be coming up in the next video. 3. Install Sublime Text and Git Bash Terminal: Okay, so we've downloaded and installed Python. Now we want to download and install the other two tools that we're gonna need the text editor and the get bashed terminal. So head over to Google. Just type in sublime text, and the first thing that pops up is sublime. Tex dot com Go and click on this, and this is our text editor. It's completely free, and you can absolutely feel free to use any text editor that you want. If you already have one that you like to use visual studio code or, I don't know, no pad plus plus any real text editor at all, you can use that. I like sublime text. I've been using it for years. It's probably one of the most used text editors for coders, completely free, and it's available for Windows, Lennox or Max. So whatever kind of computer you have, you'll see the download button for that kind of computer and you notice it's the blind text . Three. Build 32 of seven. If these numbers change by the time you watch this video, no big deal. Just download the latest one doesn't really matter what version it is, so go ahead and click this. Download it for Windows. And you could say this anywhere. I'm just gonna save it to my desk. That's a top right now. You could save it. And when it finished down looked like I'm on Firefox. So it's downloading up here. If you're on chrome again, it will be down there. Click it when it finishes. Now, this is the installation is very simple. It's just one button click next and then click install. Now, I've already installed this on my computer, so I'm not gonna go through it again. But it will just automatically install after you click this button and you're good to go. So I'm just gonna cancel out of this not to run sublime text. Just click on your Windows Start menu, type in sublime, and it'll pop up. Kind of looks like this. We'll get into all of this later on. But that's all there is to it now. We also need a terminal, something to run our commands on. So I like to get bashed terminal. If you're on windows. Windows comes with a command terminal, a command prompt. It also comes with something called Power Show and you can use those, but they don't work as well. And you can actually have some problems with version control and stuff using those. So I just recommend that you use this Get bash one. It's completely free. If you're on Lennox or Mac, you already have a terminal. You can feel free to use that. Just go up to the little search function on your Mac er, Lennox typing terminal. It'll pop right up, and most of the commands are gonna be the same. There's gonna be a couple that are different right at the beginning on try and point those out when we get to them, but otherwise it will act the same is this one fact That's one of the great things about this. Get bashed terminal. It's a bash terminal, just like on macro Lennox. So here we are, 2.23 point zero, and this is get desh SCM dot com for slash downloads site that we want and notice there's a macro Lennix version. So if you want to run this on Mac, relax, you can absolutely downloaded to follow along if you like or, like I said, just used the terminal. That's already on your computer so good. And download this. This is a pretty slow website. It always takes a little while to download. For some reason, save this any worm and just going to save it to my desktop and you could see its 45 megabytes or so. Now, this should download instantaneously on a high speed Internet like I have, and you probably have For some reason, this website is always slow, always takes a while to download. So whatever this has been 15 20 seconds or so drives me crazy. But what you gonna dio? Okay, so once it's downloaded, go ahead and click on it and hold this over. Now, this installation is a little bit weird, so I'm gonna walk you through it. So there's the license. Take that in the and then we can click next again. Now, the very first thing it asked for is what text editor are you using? And we have to be using sublime text, which is the default listed here. If you're using something else, you can come in here and click it if you want, But don't feel like you have to do it. And if your text editor isn't on this list. Don't worry about it. Because this in our text editor do not work together in any way. So there's some integration, you conduce, but we're not going to do that. So it's just sort of silly. I'm not even really sure why they asked that. So just go ahead and click next. Leave it as it is. Even if you're not using sublime, just leave it as sublime. It really does not matter. So go ahead and click next, and we're gonna have a bunch of screens here and we just want the defaults for all of them . So I'm just gonna click next, Next, next, next, next. And we don't care what any of this stuff is. Just next next, And then finally get to the install button going click that finished. I've already installed this on my computer, so I'm not gonna do that. You can go ahead and do that when it finishes installing, go to your windows, start menu and type and get G I t right or get bash, and it should pop right up when you click on it. Uh, this is what it will look like. And in the next video, we'll talk about what this is and how to use it. All the good stuff. And, uh, that's all there is to it. These are the tools we need. We don't need anything else. There's some things we'll have to download for Django, but we'll do them from the command terminal from the prompt right here. So there's nothing like we have to go to a website and download and install. From now on, we have all the tools that we need. So that's all for this video in the next video will jump in and start building out our project. 4. Set Up a Virtual Environment: OK in this video we're gonna set up and start using a virtual environment. And it's important when you're using Django to use virtual environments. And if you're not familiar with them, they're just like little walled gardens inside of your computer. So inside of our virtual environment, we're going to install Django installer, project all the things, and it's good for a lot of reasons. You can use different versions of Django inside of your virtual environment and then, you know, outside in your regular computer, there's no Django installed at all, so you might have an old website that requires a certain version of Django. You can install just that version in your virtual environment, and it's good for a bunch of other reasons, too. It's just standard best practices. So head over your Windows start menu typing, get bash and open up your git bash terminal, and the first thing we want to do is make a directory. It's M k d A R, and we want to just create a directory that's gonna hold all of our project files. So I want to put this in the C drive. And let's just call this, uh, Django a p I Now we want to move into that directory change directory, CD. See, Django, A p I. And if we type ls we could see there's nothing in there. Okay, so now we want to set up and start using our virtual environment. So to do this Python comes with a virtual environment that we're gonna use and to use that we just go python dash M V E N V stands for virtual environment and then name this thing. What do we want our virtual environment to be called? Well, I just usually call it them virtual environment. And we'll see why in just a second so he enter might take a couple of seconds. All right, so now if we type in ls to list of stuff in this director, we see there's this new ve envy folder. So to turn this on now we type and source and then that directory and in the scripts directory and then activate all right. And you'll notice every time I hit. Enter. Now there's this little V envy and parentheses. That means the virtual environment has been turned on. So that's on Windows, I think, on a Mac in order to turn it on you type and source, and I think it's been activate. So if you're on a Mac, that should do the trick. So now we're in a virtual environment to turn this off. We can just type and deactivate and boom. You could see that ve envy thing is gone. So turn it back on again. Just source ve envy scripts Activate and Boom! The Little ve envy thing is back. So now let's type in Pip Freeze, And this will show all of the python things that are installed and you see there's nothing installed because this is a brand new virtual environment. There's nothing in this virtual environment. There's no python. Things have been installed, so we have a fresh sort of brand new start, and that's exactly what we want. So that's all for this video and the next video. We'll go ahead and install Django and some of the other tools that we're gonna need it for the course, and we'll do that straight from the terminal will be super quick and easy, and that will be in the next video 5. Install Django and the Rest Framework: okay. And this video, we're going to install Django and some other things the Django rest framework that we're gonna need for this course. So we're in our virtual environment and in to install Django, we just type in Pip. Install Django. Now, if you want to install a specific version of Django, you can type equal, equal and then one point whatever to point whatever. Whatever version you want Just designated that way. We just want the latest version. So Pip install Django and Pip is the python installation processor. I don't know. Program something Pip. It's just what you used to install things with python, and you can see it's downloading. Django. It's also installing the sequel Parses Pie TZ and Django itself. Pie TZ is a python time zone file Does some things behind the scene as sequel parse Sequels A database that doesn't database stuff. Uh, that's all just normal. Okay, We also need the, uh, Django rest framework in order to create arrest ap I so same command Pip Install as just Jingo rest framework. So it's collecting. Installing. Done. All right, so now we can type in Pip frieze if we want to see what's been installed inside of our virtual environment and you knows we've got Django. We've got the Django Rest framework. We've got this pie Tizi, and we got sequel parts, not check. Take a look at this. If I deactivate my virtual environment, turn it off and then run pit freeze. Now this is saying what's installed on my computer. I've got a whole bunch of stuff installed, but you'll notice there's no Django. There's no Django Rest framework. There's no anything, right. Sequel. Parse isn't in there. None of the things we just installed appear to be on my computer. And that's because, like I said in the last video, it's a walled garden are virtual environment. So we've only installed these things inside the virtual environment. So that means every time you open up your terminal and you want to start working on your Django project every single time, you need to come to your see Django AP I directory and then type in that source V and V scripts. Activate command, and then you turn on your virtual environment and then you know we can see. Sure enough, all those things that we installed are still there and you're good to go. So so I think that's all for virtual environments. In the next video, we'll go ahead and start our Django project and start building it out. 6. Start Our Project: Okay, so we've got Django installed. Now it's time to start our project. So to do that said over your terminal, clear the screen and just type in Django Dash ad men dot high and then start project, and then name it. What? We want to name our project. Let's just call it Django. A p I All right. Now, if we type in ls to list the stuff in our directory. We have this Django ap I directory. So now we need to move into that directory CD Django, a p I. And now when we list the stuff, we see this manage dot pie file. This is the directory you always want to be in from now on in your terminal. Uh, whatever directory has your managed up I file because we use this managed up. I file to run Django Command. So you have to be in that directory. So All right, So now let's head over to our sublime text and let's add this project to supply. I'm so click on project and then add folder to project, and then you just want to navigate to your C drive and find that Django ap I directory double click it and then find the second Django AP I directory inside of it single click it just to highlight it and then click select folder. And when we dio boom, we see our jangle projects. So the first thing we want to dio is head over to are setting Stop, I file. Let's get rid of these comments. We need to come down here to installed APS and add the Django framework that we installed in the last video. So to do that, we just come here and type and arrest underscore framework. Hey, you should put your comma after it because we're gonna need Teoh. Add another installed app, probably in the next video, So Okay, so our project is up and running now. The next thing we always want to do right at the beginning is push our migrations When Django is first installed, it has some ad men user, uh, database migrations that need to be pushed. So to do that and get bash, we type in win P T y and then python manage dot pie and then migrate and move a boom. It's all been migrated. Okay, so now we can run our server to make sure that everything's been installed correctly. To do that, we just have been python managed up. I runs server and okay, it's starting to work. So now just head over to our Web browser and go to a local host. Colon 16,000 is the port that Django runs on, and we use a congratulations. We've got this little rocket and the installation has worked successfully. So that's really all it takes to start our first jangle project and, uh, get everything set up in the next video. We'll go ahead and add another app to our project and start building this thing out. 7. Create an App with Startapp and Create URLS.py: Okay, so we've got our project up and running. Now we need to add an app to it so that we could start building it out. So come back to a terminal and we just want to python manage dot pie, start after and then name the APP. So what do we want to name this app? Well, for this, a p I that we're gonna be building out, we're going to sort of create a catalogue of my online courses, and that way we'll be able to sort of see all of my courses in an A P I format. Now, there's a bunch of different reasons why we might want to do this. Doesn't really matter what FBI you're gonna create. I just happen to be creating courses because I have a whole list of courses and for an A p I you need a list of things, so I thought that would be a handy thing. So I'm gonna call our AP courses because it's gonna list is gonna be an A p I that lists all of my online courses. Really some of them. Right. So start. So python managed up. I start app and then courses. And then now we can go back to our sublime text and we see this courses directory and inside here there's all kinds of stuff. So the first thing we want to always do is go to our settings that pie file and add that new app so here and installed APS we just type in the name of it courses was common. The end okay, and that's pretty much it. So the next thing I always like to do is play around with the euros dot pie. So our new courses APP, doesn't have its own your URLs, that pie file. And, as you probably know, the girls, that pie file is the thing that creates you. RL's for your app so www dot your site dot com slash courses that slash courses. That's the girl that you need to create. And you usually do that in the Euros, not pie file. So we need to do a couple of things here in this u R l stop I file, we wanna add include which allows us to include other files. So that's good. Alice school. I hadn't copy all of this and let's create another euros that pie file in our courses directory. So just right. Click on the courses directory. Create a new file and we can paste this in here. Goto file. Save as and just call it urals dot pie. So now you'll see there's a euros that pie so you can close all these. I see this. There's a euros that pie file in our courses directory. And there's a euros that pie file in our Django AP I directory. So let's go to this one in the Django AP I directory. We need to make a quick change here. We need to include that new one in the old one. So to do that, we just go half and then we want it to be the root. So double single quotation marks and then just include and then inside here, the name of that directory courses dot your l's and I was put a comma, The end, I didn't say that, and we're good to go there so we can close this now. We pretty much won't do anything else. I don't think in this you URLs that pie fell anymore, but we will do stuff in our courses, your URLs, that pie file for one. We could get rid of this at men thing. We don't need that and let's get rid of this path. This is to the immense section of the website. We'll look at that in the next video. We'll get rid of that now. Here is where we'll put our euros going forward, but right now we don't have any and that's fine. So go ahead and save this for now. And I think we are ready to go. Let's see the last thing we need to dio and we usually do this later. But we're already in your old stuff high right now. We need to import from this directory well from this directory import views and we'll see why this is important later on. This just allows us to pull in our views, which is this file right here. There's nothing in there right now, but there will be pretty soon allows us to pull that into our Euros, that python. We'll see why that's important later on. So all right, that's pretty much it for now. In the next video, I think we'll look really quickly at the Ad Men section of the website and go from there 8. Django Admin Area: OK in this video. I want to look at the min section of the website. And if we go back to our original Yarl's that pie file, we could see we've got this. We're importing this if men thing, and we've got this path to and men. So what is that? Well, Jingo comes with an administration area on the back end that you could do database stuff. It's a nice little thing if we actually come over here, will first go to our terminal and let's run our server. So python managed at PI runs server and then head over to our web browser and go to a local host, Colin 8000 slash at men. And you see, here is the log in for the administration area. Now, we don't have a logging yet. We haven't created that. So let's go ahead and do that in this video right now. So I'm gonna come back here and control, see, to break out of our our server and we just want to win. P T y hi thon manage dot pie. Now I'm typing. Win pt. Why? Because you have to do that. Forget bash. If you're on a mackerel. Lennox, you don't have to type win P T. Y. You just type python manage dot pie, but for us and get bash. Also, if you're using a different terminal on Windows such as Power Shell or something, you won't have to type. And when Petey Wise just to get bash thing for some reason we have to do that. So all right, we need to create Super User. So the commanders win. P T Y Pant Python managed up. I create Super User, and it's asking for user name. I like it. Men's Oh, I'll just type that in email address. You can put your email address, or you can just leave it blank and hit. Enter and password type in a password. Now notice. I just typed in my password, but nothing appeared on the screen. That's just a security measure that's normal. So go ahead and inner didn't type it again. All right, so are Super User was created successfully, so now we can python managed up. I run the server again and head back to this in men area and how we could type in our our new super user user name and the password and boom. We have this cool little administration area, so there's nothing you really in here now if we click on users, we could see the user we just created right There is my email address. And if we click on this, you can, you know, add some stuff you want to add your name. I don't know why you would want to, but you can and, ah, see save. You can also click on this and delete it. If you want down here, you can change the password by. Let's see, right here there's a little link. So I ask you for your password you got type it again that you could change the password. Very cool. Uh, granted, there's not much going on here right now, obviously, but later on, this will be very, very useful toe used to create our A p I. Because the a p I requires a database to store the A P I stuff in, and this allows us to interface with the database very easily. You know, we can click. Add user will weaken. Click a button later on to add things to the database for our e. P I. And just super useful. So that's the in men area. Not much to see their now, but later on it will be cool, I promise. And I think that's all for this video. So in the next video, we will start building our model for R A P I. 9. Create Our Models Class: in this video. We want to start to build out the model that we're gonna use for R A. P I. And you know, we're gonna create this A p I and I sort of mentioned it earlier. It's gonna be a list of my courses and it'll have, like, the course title. It'll have the type. Of course it is. So it teaches Ruby. It teaches PHP. It teaches Java script, whatever language it teaches, and then it will have, like, the price. And we might add some other stuff. But each of those things needs to be saved into the database so that we can then serve it up as an A P I to other people later on on the website. So to do that, we start out. It's actually incredibly simple. We just go to inside of her courses directory, find out our models dot pie file, and you can see there's nothing in there now and get rid of that comment. And now we just need to start building out of class for our model. So we just typing class and then name it. So I'm gonna name this course because it's gonna be courses you tend to generally want to name your model singular. So instead of naming at courses, because then later on, you'll see like courses is because later on jango plural eyes is it in certain things? So we want to do it singular here. So inside here, we want to pass models dot model, and then we just define the things that we want to save. So I'm gonna create a a name. We want to create a course, name the programming language. And then let's just go price for now, right? So three fields weaken. Beef this up later on if we want Teoh. But here we just go models dot and then define the data type. This is gonna be a car field, uh, which stands for character fields. If you're interested in the different data types just could just google like Django data types or sequel data types, things like that we're using right now the sequel like database that comes with Django. So you could, I guess, um well, you probably google Django data types, but anyway, core field, that's a normal one. And inside here we want to pass max, underscore length and set that equal now notice. When I did this, it kind of changed, and this kind of looks like a capital L. But it's not. It's a lower case L. So it puts a little hook on the bottom of I don't know why. It's just sublime. Does that. So make sure that's lower case, and we want the Max Link to be, I don't know, 200 characters, whatever. It doesn't really matter now for each of these things, we could do the same thing. For here, 100 characters is probably enough. And for here, let's go. The prices go 10 and the length of that is like the characters so like If I put the price is $49 that's three characters. 123 If I go 49 99 right, that's 123456 characters. If I go 1 49 that's 1234567 characters. So the max we want is 10. I guess I could put seven, but it really doesn't matter. Back in the old days, it was important to map out how many characters your database used because back in the old days, computer space was very limited and memory was very limited. So these days it really doesn't matter. You don't even have to do this, probably. But we just do. So go ahead, do that. Okay, so that's pretty much is We can save this, and that's really all there is to it. Our model is now done. Now we have to make a migration and then push the migration into the database, and we're pretty much good to go. So let's head over to our terminal and control, see to break out of the server and we just go python manage dot high make migrations. And any time you're doing database stuff and it's not just Django, it's anything else really. Ruby on rails, anything. Database. Things are always a two step process. You make your migration, and then you push your migration into the database. That's just or how it works. So here we're typing this Pan Python managed up. I file make migrations, and what that does is it takes this file we just created with this class and sort of converts it into database language, right? That's what a migration is. So it's done that now. So now we just need to push that migration into the database, push that list of database language stuff into the database, and to do that, we just go python manage dot pie, my great. And you remember we did this in the very beginning of the course when he pushed in. They have men area of migration. Okay, so that looks like it worked. So now let's go. Python managed up. I runs Server to run our server again and let's head back over to our in men section and hit Reload and let's log out and log back in. And when we do, it looks like nothing has changed because we haven't associate ID our new database stuff with the admit area. So we'll go ahead and do that in the next video. 10. Add Our Model To The Admin Area: Okay, so we've created a new model, but it's not showing up in the administration area yet, so that's a quick and easy fix. We just need to go Teoh. Let's see. Instead of our courses directory, the ad men dupuy file and we just need to add a couple of things. So from dot models, which is this file right here that we just played within the last video, we want to import that class we just created, which is course in that is just what we named this class right here course. So we're saying, Hey, import the course, right? So that will import it. Now we need to register it with our immense section. So you and men dot sight dot register and then just pass in that name of that class, Of course. So if we save this and then head back over to our Web browser and hit reload boom courses shows up and you see there are zero courses because we haven't added any yet, but we can Now we can click this ad course button and this is why this and mini area is super cool. We don't have to create any python Web pages in order to create a form that we have to fill out to add things to our database. We can just use this ATM in area and it is super useful. Now, if you were creating a website where you wanted to do this on a web site, you'd have to do all that stuff. But we're not. We're just creating in any any p I that we want to update right here easily. Right now. Later on, we'll build out the A P I on the website itself, and there'll be a form there where we can add stuff and edit stuff. But for right now, we don't have anything yet, so let's just add a couple of them. So I'm gonna add a couple of my courses that I have. So let's just do a couple two or three. So one of them is, um, Python programming for everyone. The language is hi thon, And the price, let's say $49 so we can't save and at another. So next we have Ruby programming for everyone is ruby language. And again, I'm just gonna put 49 bucks. So saving at another one, we've got Java script programming for everyone. Language of that is Java script. And for that one, we're gonna say 49 bucks for that. So we go and save. Now check this out. If we go back to our home here and we see our courses section we click on this. We have this weird course object three course object to core subject one. If we click on any of them it, we can see it's the stuff. But that's awkward, right? Can we change this? And actually we can And it is super, super easy. We just head back over to our models. Stop! I file an inside of our class. Make sure your indented in, as you mention if you don't know this is a tab. I haven't hit the space key a bunch of times. This is Tab and Python is tab sensitive. So it's important that you tab where you see me, Tab. So tab over here and we can just define underscore, underscore str underscore, underscore and then pass in self And then inside of here we could just return self dot name and so basically, self is just our course. We're passing our course into this thing and we want to return self dot name? What is dot name? That's this thing right up here. Right? The name field in our database. So we save this head back over to our web browser and hit reload, unable to connect. Let's close this. So invalid. Syntetics. That's weird. Let's try that again. Oh, there we go. We need a space there away. All right, So defined space. Underscore. Underscore. All right, so save this. This should work now. There we go. And you see, if we go home, Click on courses. Boom. Now the names show up. And that's just because of these two lines of code. You can make sure they're indented correctly space here and very cool. So okay, I think we're pretty much done with this sort of Django East stuff. Now we need to do the a p. I stuff to get this working as an A P I and I think we'll start to do that in the next video 11. Create a Serializer: in this video, we're going to start to sort of build out the Django rest a p I stuff. So I'm just going to open a new Web browser. I'm gonna type in Jingo rest AP or arrest framework. And here it is. Jingo Dash rest dash framework dot org's. And this is the Jingo rest framework. When we installed this way back at the beginning of the course, when we install Django installed the Django arrest framework. And you kind of look through here if you want. And c we pip installed it just like this. Uh, there's some optional packages we did not install, cause we don't really need. We added the installed restaurant work tour installed ABS and are setting Stop. I file. And this is no longer correct. It looks like this is a little outdated, but you could check through here if you're interested. Look at development. This is a very introductory course on Jango rest AP eyes. You want to dive in in more detail after you finish this course, this is a good place to go. There's some quick start guides and other guides that you can look at, has some stuff you know, But for now, we'll just leave that as that. So for us, the next thing we want to do is create a serial Isar. And this sort of takes the data from our database and serialize. Is it into, in our case, Jason, which is what most AP s used to send data around the Internet? So a serial Isar it doesn't just it creates. It translates our data to and from Jason. So it's just a important thing. So in our courses directory, we just want to create a new file and let's go file, save as. And let's say this as serial eyes are s serialize ear's dot hi and I spell that correctly serialize er's yes and inside of here we need to from arrest underscore frame work. We want to import serialize er's cereal eyes. Irv's okay, and then we need to also importer model. So from dot models, import course, which again is just our models. And in this course right, all right, now, inside here, we need to create a class and let's go class, and this is gonna be course serialize er singular. And that's gonna use cereal eyes, ear's dot model serial Isar. So did I suppose correctly serialize ear's dot model capital M serialize er's capital s serialize er And again, this kind of looks like a capital l right here. But it's not just sublime doing. It's weird thing. All right, so inside here, we need to pass a class of meta and inside of that, we need to tell Tell it what model to use. So the model equals course, which is just this guy, of course, of course, course. And the fields we want in our a p I r. Fields and set that equal to and this is just a couple of all the fields that we want. So we want an I. D. And this is just a number. We didn't put this in our model, but it does it anyway because when we put things in a database, it always gives it an I. D. Number. So each item is an I D number has an I. D. Number automatically given to it. So we want to use that. We also want to use the name and we want to use the language and we want to use the price right, and these things I'm taken straight out of our model, right? So that's name, language and price here were saying, used the name, the language in the price, right? So go and save this and that's pretty much it. So our serialize er's air done are serialize. Er, I should say is done. And in the next video, we'll go ahead and start building out our views for the actual Web pages that the FBI will create for us and that will be in the next video. 12. Create Views: Okay, so we've got our serialize er now we need a view. And the views in Django are always the Web pages, right? And the views air sort of the brains behind the scenes that tells Django what to do with the Web pages. Well, normally you have to create a view or create a function for each web page you want to build . But we're using the jangle rest framework, and it builds it all for us. We just need to sort of designate that and, um, do a couple of things. So interviews dupuy file. We want to import from arrest, underscore frame work. We want to import view sets and view sets or just what they sound like. They're sets of pages that it's gonna create for us. And we also want to from dot models import, of course. And that's just our model that we created earlier. And then we also want to import from dot serialize er's, which is the serialize er's file we created in the last video. We want to import course serialize er right. And that wouldn't say this is just this thing right here. Right? So we're importing this class basically right. Okay, so that's that. Everything is imported. Now we need to create a quick class and let's call this course view. This is gonna be from the view sets, I guess. And we want to import the view sets dot model view set and make sure models capitalized. View is capitalized. Set is capitalized, Colon. And then inside of this view, we need two variables. We need a query set set that equal to something, and we need a serial Isar underscore. Class said that equals something. So this is just gonna be equal to that right course serialize er which we imported from serialize er's, which is this thing, right? So we're saying, Hey, what serialized class should we use this class? Right, that makes sense. And the query set is the model. It's our database, and what we need to do is pull the data out of our database. And to do that with Django is just super easy. We just call the name of the model, which is course, and then we want to call the objects in that model, and then we want all of them, right, so that's pretty much it. So what we got here. We've got we're gonna be creating websites or what pages? And the stuff that we're gonna put on those Web pages is our query set stuff. It's stuff from our course model. What stuff? All of the objects. Right? Well, what's our model? What's our course model? That is just this stuff. So we're gonna pull all of this stuff from the database, right? And serialize her. We're going to use serialize. Er is the thing that converts that the database stuff to and from Jason is the course serialize er, then we created in the last video so much of stuff here. But it's really very, very simple stuff, and that's really all there is to it. So we're almost down. Go ahead and save this. All we need now is u R L to handle this stuff, and I think we'll look at that in the next video 13. Create Urls and Routes: Okay, so we've got our views. Now we need are you RL's. So head over to our your old stop high file and make sure you're in the 14 courses, not the one in Django. AP I back. We can close that one up. So inside of here we just need Teoh do a few things. So first off, let's go from rest underscore framework. And just like with the views, the rest framework is gonna do most of the work here. So from rest framework, we want to import routers and a router allows us to create, get and post requests and a get request gets a thing. It shows it on the screen. Ah, Post request post something to the database. So, you know, earlier we added my three courses on the admin area. Well, we can also do that from the actual upside itself. And these routers will create that functionality for us, which is super cool, because that's sort of a pain in the butt to do on your own and to have this done forces very cool. So to set this up, we need to create a variable. It's call it router and said that equal to routers dot Default router. Right? The fault router, If that's bold, right, And then we need to register this router, so register dot around did not register, and then it's gonna be courses. And in the view, dot course view. All right. And this is how we're gonna access it from the website. Right? This is the you, Earl we want to show. So if we come back here, you know, remember local hose way. Is this even running anymore? Yeah, but it's gonna throw an error. I think you know. So from now on, when we want to access this ap, I will go to courses, right? We can't just yet because we're not done building this out. But when we do, that's what that does right there. So that's pretty much it. We just need to not create a you earl pattern here. And to do that, we just go path and we want the home page. You know, the index page. We want to include router dot your l's. And what a calm if we want have to. That's pretty much it. So now if we save this and come back to our main local host may have to restart our server . That's control. See, to break out of this, we're getting an error View dot courses view. Oh, that should be views. Dot course view in our router register. All right, so save this. Now come back here and let's run our server again. Now, if we go to a local host Colony 1000 Boom. We get this website that's already built for us. Very, very cool. And check it out. It's our A P I root and has this logo hosts for such courses. We click on that. It lists all of our things. Now why courses again? Because we just now did this right here. If we want to call this anything else Elder, if we save this, come back it reload Now it's Elder C, but that's stupid s. So it's had a change of back to courses. Very cool. Save it. Reloaded courses and check it out. This is a python list inside of the list. Each item in our databases a dictionary we can tell by the squiggly lines has an I. D. Ah, name these air key value pairs. The key is the I d. The value is one. The key is named. The value is python programming For everyone. The key is language. The value is python, so we can access all of those things. So, for instance, here, number two Ruby programming for everyone. If we want to just access that, we can come up here to courses slash to and then, boom. That appears. So this is very, very cool. And just like that, we're done. R A p I is basically finished at this point. So, um, take him second to kind of look through here and play with this. We've got the html down here or the raw data. This is the actual content that's going to be served up by Jason. All right, Very cool. You can go back. Same thing down here for a neat and very cool. So I think in the next video, we'll play around with this a little bit more, and we'll test this out from 1/3 party that will connect to it from the third party to make sure that all this stuff actually works. It will start to look at that. Maybe not the next video with the video after that, but that's coming up 14. Switch To Hyperlinked Serializers: Okay, so our a p i is up and running. We've had this little website created for us. Very cool. And, ah seems to be working very well in this video. I want to just tinker with us a little bit. And then I also want to change something in here to add the actually you URL for each of these things. So, for instance, if we go, if we want to see this object right, we can go to three, which is the i, d and C there. But I'd also like to add that as a your all inside the data that's returned. So she had to do that. But first, let's actually go to that one. Analysts we could see we have we can get get the Jason, just just this very goal. Or we can get the actual a p I. Which is what we're already getting. Which school there are options. Play with that. We can delete this guy if we click on options. I know delete button disappeared. Start over again. Let's go to three. Weaken. Delete it if we want. We don't want to hear, though we can also put put means to sort of post, right? So if we wanted to change this from Java script to just Js programming, we could put it and then boom, You can see it's changed if we go back to the course list. It's changed there, too. We can also actually a let's go back, change that back road quick Java script. C R I p D. OK, put boom has changed. We can add other things from here. Remember, before we added each of these items from the Adminis area? Well, now we can add them here. So let's go. Html programming for everyone languages. Html. The price is 49 books. Boom there it's been added. If we go back to the course list, we should see. Sure enough, there it is. It's been automatically given an idea four and very, very cool. So it's amazing how quickly were able to do this and build us out and have this fully functioning very cool AP I that now anybody can connect to and use now I should mention anybody can connect to this course role. Right now, it's only on our computer. Before we were to push this up to Web hosting somewhere and we're We're not going to do that in this video, but I have other jangle courses that show you how to push Django projects up to web hosting . If you wanna learn how to do that, if you don't already know it. But if you do that, anybody who accesses this can add things or delete things. So in the real world, you probably want to put this behind a password. You know you wouldn't want only want people that have the password to be able to do that. I also have user authentication course for Django that teaches you how to create websites that have passwords and users that can log in and out if you want to learn how to do that. But that's sort of a basic thing. That's not really doesn't really apply to just building the A P I and this intercourse that we're learning right now, So I'm not gonna get into that. But anyway, uh, this video okay, it's getting a little long, but I think we have time to I want to add the you URL for each of these items. So, for instance, if we go to courses slash four I want to put this. You are all inside of our a p i itself. So how do we do that? Well, it's actually not too tricky. We just go back to our serial Isar and we just make a quick change here. So serialize er's dot model serialize er we can also we could change this to hyper linked and that's a lower case. L hyperlinked Smail serialize er and then down here we could just add a euro. Right? So if we save this and come back to our website here and hit reload boom, Now the earl has been added and it's even clickable, which is kind of cool thing. So, you know, maybe you like that Maybe you don't, but I think it's cool. So I just sort of added in there, So Okay, everything seems to be working now. Normally, you're not gonna want people to be able to see this stuff. That's why I mentioned the, you know, sort of member password area. But somebody who just wants to use this thing, how would they use it? Will will start to look at that in the next video 15. Install Postman: Okay, so our FBI seems to work. But we've got this whole web page here, right? So normally, if you connected to an A P I from some other thing, it would look like this, right? And it does look like this when we click that button, but we need to be sure that this thing is actually working. Right? So how do we do that? Well, we need to connect to it from some other third party thing to make sure. Now I've got lots, Of course, is that I've created on how to connect to 1/3 party AP I from a Django project. But this is an A p o. We've created ourselves. So how do we connect to it from ourselves To make sure it's really well, we need to use some other services or software or something to connect. And that's what we're gonna do in this video. So I'm going Teoh. See, Let's go to Google and let's look for a postman, a P I and Postman is just a little Prada are just a little piece of software that will act like a client and connect to your a p I and sort of tested to make sure it works. Right? So this is the website. Get postman dot com. Uh, get postman dot com slash downloads to download this app, and it's available. It looks like for Windows. I don't know. I think Mac and Lennox Yeah, there's a Mac and Lennox version if you want, but I'm going to download this for Windows. So let's go Windows 64 bit. And we want to save this saving anywhere. I'm gonna save it to my desktop. Now I'm on Firefox in. My downloads are up here. If you're on chrome, I think your downloads air down here in the corner. Wherever you need to go to find it, click on it to install this thing. So it's just run this real quick and it's installing postman. It takes a minute or so. Okay, so now hold this over. Play with this a little bit. All right? So I think the first time it asked you the law again, you have to create an account. I just log in with Google, or you can create Eric and account it. Um, it's completely free. So let's see to close this. I think this is just the basic area here and all right, this video's going a little bit long, and we want to spend a few minutes actually playing with this. I'm gonna stop this video and the next video. We'll actually take a look at this and start to tinker with it and see if our A P I actually works. And that will be in the next video. 16. Use Postman to Confirm Our API Works: Okay, so we installed this postman program this software, and in order to run this just got a Windows Start menu and type in Postman. Postman, are you say that and it'll pop right up, so all right, here we are. And we could just go toe and weaken, designate what we want. We want to get request, and we could just enter in the U Earl of our app. So that's just local host 8000. This guy good and close that. And when we click, send it sending a request and boom, We can see this is what gets returned as the a p I. So if you build some other programs, some other software that connects to your a p I using a request method or or whatever, this is what will be returned to you. And it's exactly what we think it should be, right? It's a python list inside of the list. Our items that are dictionary separated by commas right inside of those dictionaries or key value pairs with our database stuff. It has been converted into this a p. I is very cool so we can do Jason. We could look at the h xml. You can look at html Very, very cool. Just basic text, right? So very, very neat. So what else can we do here? We can dio we could look up a specific item. And when we do that boom, that specific item shows up, which is also what we would expect to see our a p I works. Oops. We want to get We've verified it. The third party, 1/3 party client that is could be able to connect to it and, uh, it returns. What? It should be so just ridiculously easy to do this and, you know, wanted to take us, like, really 10 minutes of actual coating to build our own ap I Now, obviously, you can get much more detailed into this and do all kinds of crazy things. But for the center, of course, Just building a basic AP I that just shows some basic data. I think this is really, really cool and, uh, very cool. So in the next video, we'll go ahead and set up version control so we can save her code and push it up to get home so you can look at the code. Uh, that I used to create this course if you need to and will start to look at that in the next video. 17. Set Up Version Control With Git: Okay, so our app is basically done. Now we want to savor code version control and push up to get up really quickly. So we'll do that. Take a video or so to do that, maybe two. So that'll be fun. So let's head over to our Web browser. Go to co to me dot com four slash get. And these were the commands that you need in order to initialize set up in initialized. Get on your computer and I can never remember him. So I just put him on this web page, and I just copy and paste him the first to you need to put your name and your email address , but the rest of them would you just copy and paste. So let's go and do that real quick. So I'm gonna right click and paste and then put my name John Elder. And then I'm going to grab this one control, see and then paste John at kota me dot com. All right, now we just need to copy and paste the last three and paste and again, I don't remember even what the's commands do. I just remember this is what you have to do to set up get the first time done this? Thousands of times. Probably hundreds, at least. All right, in the last one. Get in it. So, uh, Boom. So we see this master, That means get has been set up and turned on. If you're not familiar, get is a form of version control that allows us to save versions of our coach. Just standard best practices. So okay, now we want to add over code to our new git repository. So get add, period, get commit, dash a m. And let's call this initial commit. Any time you commit stuff, you need to add a little message to say what you're committing. Okay? And then that's pretty much it. So now we want to push our code up to get help. If you don't have a get help account, go ahead and create one. It's totally free. But first we need to create an ssh key. So to do that, let's go M k d I r. And we want to put tilled forward slash dot Ssh. All right, so now we want to move into that directory. So seedy, tilled forward slash dot Ssh and the dot makes the directory hidden. That's just what we do with ssh keys. So Okay, we're inside that directory. We could see ls There's nothing in it. So I put on the screen. Now let's generate our ssh keys. So let's go key. Let's go. Ssh! Dash key gen dot e x e. And I think that command is different. If you're on a Mac, you could just google how to create an ssh key on the Macintosh. You'll find the command immediately. So where do we want to save this? Want to savor right here? Let's create a password. Now, just leave that blanket. Enter. I don't want a password. Do it again. Okay, so now if we type it now last week in C, these two files have been created, and what we want is the dot pub one. So let's go. I d underscore r s a while. First we want to go cat, and then I d underscore our a se dot hub. And this is our ssh key. So just sort of highlight all of it here and then right. Click and copy it next. We want to go to get help dot com and if you don't have an account, go ahead and create one and then go to settings and then ssh in gpt keys. And we just want to add our new ssh key, right? Click and paste. You don't even have to put a title like Enter. It will ask you for your ah, test word again. And boom, we're done. So now, uh, see, we want to push our code up to get hub. This video is getting a little bit long. I think we'll do that in the next video. 18. Push Our Code To Github: Okay, so we've got our get hub account here now. We just want to We're in our repositories right here. See, I've got very many, and we need to create a new repository. And let's call this. Ah, Jingo, ap I whatever. And this could be public or private. I'm gonna leave it public so you can see my code. If you need to look at the source code or whatever, you can find it here. So it's create repository. And now we need to add these two commands down here toward terminal because we're pushing an existing repository that we created in the last video from our terminal up to get hub. So I'm just gonna copy each of these and go back over to a terminal and notice we're in our ssh directories. We need to change back into our Jingo ap I directory. And then we need to also change back into our second Django a p I and we could see we're in the right place because we see this master. That means get has been turned on, right? And if we type in ls we see are managed up. I file Remember, we always want to be in the directory with our managed up high file. So Okay, so now we could just right click and paste and whips. That is not the command we want. Pay stands. Go back over here and let me copy this again and paste it in. So that's the 1st 1 And then we wanna do the 2nd 1 copy based, and this will push all of our code up to get home. Yes, we want to continue. Okay, so now we come back here and hit. Reload. Boom! This is our entire Django project. Now on, get hope so very cool. And like I said, if you want to look at the source code from this course and for whatever reason, there wasn't a whole lot of it. But if you dio, it's gonna get have dot com for its lush flat planet for a slash Django a p I and you noticed this initial commit That's that little commit message we did in the last video. So we can actually changed us, Play around with this a little bit. So let's head over here to our serialize er's that pie file. And I'm just gonna create a comment. This is our serial Isar Paige. I don't know. So go and say this. Our code has changed. Now let's save it all to get Hub one more time. So from now on, any time you want to save your code to get hub, you would type and get add period again. Get commit. Dash A. M added comment to Siri Serialize er stop I and then to push this up to get help. From now on, just get push. All right, so that's done. Now we can go back here and hit reload, and we see up. There's that new comment we just typed added comment to serialize er's. If we click on here, you can see up. Here is the actual file. We just changed and there it is. So that's get that's good heavy. I'm sure you already know this stuff is just a quick review. Also, I just wanted to push our code up here so it's available over in case you want to reference it in the future. And so that's cool. So I think that's pretty much it in the next video will spend a minute kind of looking through this stuff again. going over the code one more time very quickly just to touch on some things and then we'll wrap this up. 19. Review The Project: All right. So we flew through this course, This was very quick, but it could be right. This was pretty easy. There wasn't a whole lot of moving parts, you know, just go through. Hero. Quickly. We started out creating our project. Django, FBI. We added our rest framework, which we pip installed, and our courses after we created that app. Right? That's pretty much all we did here. We created a you, Earl. Stop. I file in our courses, AB, and we tweaked the original yarl's that pie file to include This includes statement which pulled in our new euros that pie file, which is this file right here inside here. You know, we just imported our views. We did this rest framework thing. All of the heavy lifting for this project and for r a. P. I is done by the rest framework. And here we just created our routers and then included those routers as paths. And then the rest framework creates all the past. Right? So the past. But that I mean, like each of these girls, right courses, Course one course to course, three course floor, etcetera. All of those things get created automatically. And then the views. Same thing. All of those pages that air created all of that stuff just came just three lines of code, right? It's just incredible. And then the only really sort of really thing that we did ourselves was create our class, right? Our model, our model class. I should say that defined what stuff we want in the A p I. And that was just a Z Zia's creating this course class and then just creating each of these things. If we wanted add anything else, we just could just just in the exact same way you can have 100 things here if you want to do just type them out very, very easy. And then, of course, this little bit of code was, just register it on our min site and show the actual name of thing right here if you remember so ridiculously easy. And like I said, if you want to learn more about this stuff up, So Server has stopped running, uh, Python managed Top I run server that we really need to run it anymore, but it's just fun. Like I said, if you want to learn more about this stuff, Django rest framework got a Google search for that. Here's the rest framework. You could see the website even kind of looks the same as ours. They use the same layout and stuff, which is kind of cool. But, you know, come through here looking installation. Look at examples. There's all kinds of cool stuff you can read. There's quick start guide. Um, you know, dive through the docks, learn more stuff about this. But just for our purposes of building a simple AP, I it just doesn't get much easier than that. And a lot of people think AP I do. You know it's complicated. We use AP eyes all the time, but to build your own. Oh, that's hard. No, it's not hard. We I think you've learned throughout this course it is incredibly simple and, ah, just that easy. So go forth, build your AP eyes, have a great time doing it, play around with us in next video will say a couple more things and then wrap this up