Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Learn how to respond to HTTP GET requests with APIView
Now the Serializers are in place you're gonna need a way to handle incoming HTTP 0:00 request to your API. 0:05 Django REST framework provides a view class named APIView, which subclasses 0:06 Django's view class but with a few minor but important differences. 0:10 I'll include a link to the API view documentation in the teacher's notes. 0:15 The main difference that you need to be aware of, 0:18 is that the requests that are passed to the handler methods of APIview, 0:20 will be REST frameworks request objects, instead of Django's HTTP request objects. 0:23 REST frameworks request object is an extension of Django's standard 0:29 HTTP request, which adds support for 0:32 flexible request parsing and request authentication. 0:34 This means that by using the request object you can treat JSON data and 0:38 requests the same way that you would normally deal with form data. 0:41 Again, check the teacher's notes for 0:44 a link to the full documentation on REST frameworks request object. 0:45 There's a lot to see, so I'll meet you in workspaces and 0:50 you can start building your first API view. 0:52 Okay, so I'm here in views.py in the courses app, 0:54 to start writing these API views. 0:58 And the first thing I need to do is get my imports out of the way. 1:00 So from rest_framework.views import APIView, 1:04 and from rest_framework.response import Response. 1:09 It's not all caps, Kenneth. 1:15 And then from dot import models, and from dot import serializers. 1:18 Gonna use those serializers we made last time. 1:25 All right, and I don't actually need this render import up here because I'm not 1:29 gonna have any views that render templates. 1:33 So I'm just going to go ahead and delete that. 1:35 You can keep an eye on it if you want. 1:37 It's up to you. 1:38 All right, cool. 1:40 So now it's time to write the first APIView. 1:41 So this is gonna be one that lists courses, so 1:45 I'm gonna call it ListCourse, cuz that's what it does. 1:48 And this inherits from APIView, and then I'm gonna define the get method. 1:53 So this get method will be the method that happens when a GET request, 1:59 an HTTP request with the GET method comes into the API. 2:04 All right, so it takes self, it takes request, 2:08 and then it has a format=None argument. 2:13 The format controls the format that comes back out. 2:16 So what are we gonna do inside here? 2:21 Well, let's get the courses. 2:24 So, courses = models.Course.objects.all(). 2:25 And then let's serialize this. 2:30 So serializer = serializers.CourseSerializer, 2:32 and we're gonna pass in courses. 2:39 And then so, in the last video we serialized a single course, 2:43 if you're gonna serialize multiple objects, 2:47 you have to pass in another keyword, which is many=True. 2:49 So, it's just letting the serializer know that you're sending in multiple 2:53 items instead of a single item. 2:57 And then we'll return a Response of the serializer.data. 3:00 Fairly straightforward, getting all the courses from the database, 3:06 sending them through into the serializer and 3:09 then sending that back, sending that data back out as a response. 3:12 Now I'm not done yet, though, because like all the views in Django, 3:16 I have to hook this up to a URL. 3:22 So I'm gonna make a urls.py. 3:26 Cuz I don't have one yet. 3:31 And then inside here from django.conf.urls import url from dot import views. 3:33 And then urlpatterns is a list and 3:43 my first URL is going to be that, so just empty string. 3:47 views.ListCourse.as_view, and this name is gonna be course_list. 3:53 All right. 4:01 You'll do this with any, I mean this this right here is what you would do with 4:01 any class based view in Django. 4:04 If you're not familiar with class based views, check the teacher's notes, 4:07 there should be a link there, all right. 4:10 So then over here in this urls.py we need to bring in those URLs, right. 4:12 So I will add another one here url(r'. 4:20 And this one is going to start with api/v1/courses/. 4:25 And I'm going to include('courses.urls') And 4:31 I'm gonna give this a namespace='courses'. 4:36 All right cool, make sure you've noticed this v1 on here, 4:41 we're putting the versioning number inside the URL. 4:45 Versioning APIs is a really good habit to form early on. 4:48 I mean, what's going to happen a year down the road when you need to 4:52 make some serious changes to your API? 4:55 Maybe by the time you have thousands of people relying on your API data. 4:57 Do you want their apps to break when you push out some changes? 5:01 Yeah, you probably don't. 5:04 I know, I wouldn't want to. 5:06 No one has that much time for email. 5:07 So if you version your API now you can roll out versions two, three, 5:10 four whatever and give your users a nice safe upgrade path. 5:14 There are a couple different ways to handle versioning with Django REST 5:17 framework. 5:20 There should be more information in the teacher's notes, so check down there. 5:21 All right so time to start my server up and check, 5:26 gotta be in the right directory, and check the first view. 5:31 First view ever made, all right, cool. 5:38 So let's go here and then /api/v1/courses/ and 5:40 that should get me this. 5:46 So this is amazing because this is the browsable API, 5:49 the beginnings of the browsable API. 5:52 But check this out, right? 5:54 I've got List Course, here cuz that's the name of my thing, this was a GET request. 5:56 I can change this if I wanna do something different specify a format. 6:01 Yeah, I mean, this is just awesome, this is so handy. 6:06 What would be really awesome is if I had a way to create courses from right here. 6:09 But I don't, but let me see if I can do that. 6:15
You need to sign up for Treehouse in order to download course files.Sign up