GET requests are only half (or less) of the equation. Here's how to handle POST requests, too.
Okay so I have this great hopefully to be browsable [LAUGH] API. 0:00 Right now I can't really click on anything to go anywhere new. 0:04 Anyway. 0:09 But I don't have any way to add new courses through this. 0:09 So I want to do that. 0:12 It should be fairly quick. 0:14 So I let me see what I can do here. 0:15 So I'm gonna hop back over here to my views.pie. 0:17 And let's think about this for a minute, before I start writing any code, 0:21 if I add the ability to create a course to my view, 0:25 then the name ListCourse doesn't really make as much sense anymore, right? 0:29 Because I'm not listing courses I'm creating courses, or 0:33 I'm kind of doing both cuz I wanna to list them and create them. 0:37 So, I'm gonna rename this to ListCreateCourse. 0:39 Okay so it's all I'm wanna do is rename it and then I need to add a post. 0:43 I might have to build to send back status codes so let me import that real 0:49 quick too, from rest_framework import status and 0:54 then down here let's do a def post and 1:00 this is gonna take a request and a format. 1:05 All right, so fairly straightforward there. 1:08 So now serializers can take data that comes in and 1:12 then use that to create a new instance just like a model form can. 1:18 I keep talking about these being model forms. 1:21 It's because they're really close to being model forms. 1:23 So, let's make a new serializer here, 1:26 serializers.CourseSerializer. 1:29 Serializer there we go, and the data that comes in just like the data with the form, 1:35 is gonna be request.data and then I'm gonna check to 1:40 see if the serializer is valid and I'm gonna pass in and 1:45 an argument here called raise_exception, and I'm gonna set that to true. 1:49 So the cool thing about this is if the data is not valid if something doesn't 1:54 pass validation then it throws an exception and the view just stops working. 1:58 We throw an exception. 2:03 We get a 400 bad request. 2:04 The API, you know doesn't do anything else. 2:06 So that's great. 2:09 If the data is valid though, then I wanna do serializer.save which 2:10 will save the instance to the database. 2:15 And then the last thing I wanna do is I wanna return a response with 2:18 the serializer.data and 2:21 the status should be equal to status.HTTP_ 2:25 201_CREATED because that's what happened. 2:30 Something was created. 2:35 Now I could totally just put in 201, but 2:36 that seems it's kind of magicy, so yeah, I don't want to do that. 2:40 One cool thing here that I didn't mention yet, that thing about the step-by-steps. 2:46 So, Serializer right here, we do this course serializer, we send in the data and 2:49 we get back a serialized version of a course in memory. 2:53 And then we check to see if it's valid. 2:59 That doesn't change the data at all, except it throws an exception or 3:01 it continues on. 3:04 And then here when we do the save, it sends it to the database and 3:06 it updates Serializer because when I send the course to the database, 3:09 it's going to suddenly get a primary key it's going to get an ID and if I had you 3:15 know created ad or updated ad or whatever fields, it would get those as well so it 3:20 updates the serialized thing in memory so that when I send it back out right here. 3:25 It has all the fields available that I specified in the serializer. 3:30 So that's cool, I didn't have to do that myself which is really nice. 3:34 Okay I do have to change one thing. 3:38 If you saw this error pop up you know what it is and I get to come over here and 3:41 it isn't list course it's ListCreateCourse. 3:46 So when I leave that alone or 3:50 not leave it alone I want to update it that's what I did. 3:51 I'm not gonna change this though. 3:55 And then let's come back over here and let's refresh this page. 3:57 So now I have a ListCreateCourse because that's the name. 4:01 Did you notice that before, cuz that's kind of cool. 4:05 ListCreateCourse, ListCreateCourse. 4:07 I didn't tell it to do that anywhere. 4:11 Okay, so I've got get and post as my allowed methods. 4:13 I still have the two items, so that's cool. 4:18 If I scroll ahead to the bottom, now I have everything that I had before, 4:20 which is this form, this form is is kind of neat, 4:24 because I can use this now to create something, 4:29 right, you remember earlier I set the default authentication permissions, 4:33 to be the one where, if I was authenticated I could do stuff, right. 4:38 Well I'm authenticated, so I can do something. 4:42 If I was to take this though and open up a new incognito window, I've gone incognito. 4:46 Where I'm not logged in, right. 4:54 I get this login thing right here and I scroll to the bottom there's no form. 4:56 Because I'm not authenticated, right. 5:00 So, am I unauthenticated user? 5:02 Can't do anything but my authenticated user can do 5:05 basically whatever it wants and in this case it can post to the form or 5:09 to the view and that will let it create a new thing. 5:14 All right, cool so let me go ahead and create a new course. 5:17 So I'm just gonna add in, let's just add in title. 5:22 And I'm going to do Object Oriented Python. 5:26 All right and I'm going to post that and I've got a bad request. 5:31 See right here HTTP 400 Bad Request and 5:37 my message is that URL, this field is required. 5:40 Okay cool. 5:44 I can do that so title, Object-Oriented Python and 5:45 I'm gonna test my other validation that I had in there and I'm gonna send the same 5:52 URL teamtreehouse.com/library/python-collect- 5:57 ions which is totally there on the URL I'm just copying that real quick. 6:04 Turning on to the URL, but I have a unique equals true on the URL so 6:08 this should throw an error as well. 6:12 Let's find out. 6:14 Again still 400 bad request and of course this URL already exists. 6:16 So that's awesome that I'm getting like valid information there, 6:20 so objectorientedpython pretty sure that's actually URL. 6:26 Let's post that and now I get back to a one created and 6:32 I get the individual item that I created. 6:36 So that's kind of neat that I get back the handy data. 6:40 I get back just what I wanted so awesome. 6:44 And let's see what happens if I do a GET on this and now I have one, two, 6:47 and three courses so awesome. 6:51 It updated the database just like I wanted. 6:54 Handling both GET and POST requests was pretty simple with API views? 6:57 Having a class that we can simply add methods to for each of the different HTTP 7:01 verbs we want to handle definitely makes it cleaner to extend our views for 7:05 different needs. 7:08 I wonder though if there's a quicker or 7:10 easier way to create API views with Django REST framework. 7:11 I bet there is. 7:14
You need to sign up for Treehouse in order to download course files.Sign up