Build an API with Django // Part 1 - What is an API?

Matt Freire
14 Oct 201916:28

Summary

TLDRThis video introduces APIs and Django REST framework for building APIs. It explains what APIs are, their purpose, and how they allow different applications to communicate. The tutorial then demonstrates creating a simple API with Django, using JSON responses, and setting up a project with Django REST framework. It also covers the basics of API views and responses, providing a foundation for further exploration in the series.

Takeaways

  • 😀 Introduction to building APIs with Django Rest Framework (DRF) and the basics of APIs.
  • 📘 API stands for Application Programming Interface, enabling two processes to communicate through programming.
  • 🔄 APIs allow services like websites and mobile apps to interact with a central service to send and receive data.
  • 📡 APIs return data in formats like JSON, which is used to exchange information between systems.
  • 🌐 APIs are essential for integrating multiple services and applications, like web and mobile apps.
  • 📋 REST APIs return data only (no HTML or front-end components), allowing developers to use that data in various applications.
  • 📦 Django Rest Framework simplifies building APIs by providing built-in tools like serializers, views, and authentication methods.
  • 💻 Django’s JSONResponse is used to return data from APIs in JSON format, which is the standard for API data exchange.
  • 🔧 The series covers topics such as authentication, permissions, serializers, API views, and integration with front-end frameworks like React and mobile frameworks like Flutter.
  • 🚀 DRF provides a user-friendly interface for working with API responses, making API development faster and more intuitive.

Q & A

  • What is the main focus of the video series?

    -The main focus of the video series is to discuss APIs, specifically building APIs with the Django REST framework.

  • What prerequisite knowledge is recommended before diving into the Django REST framework?

    -It is recommended to have a good understanding of Django itself, including views, URLs, templates, and forms.

  • What is suggested for those who are new to Django?

    -For those new to Django, it is suggested to start with courses that help understand Django basics before moving on to the Django REST framework.

  • What does the acronym API stand for?

    -API stands for Application Programming Interface.

  • What is the primary function of an API?

    -The primary function of an API is to allow two processes to communicate with each other by sending and receiving messages.

  • Why would someone use or build an API?

    -People use or build APIs to share applications with others, integrate with services, or to enable communication between multiple services.

  • How does an API differ from a normal website?

    -An API differs from a normal website by only providing the data, typically in a JSON response, rather than rendering HTML, CSS, JavaScript, or images.

  • What is the significance of returning data in JSON format from an API?

    -Returning data in JSON format allows for easy integration with multiple services, as the data can be manipulated in various ways by the receiving application.

  • What does the Django REST framework offer that normal Django does not?

    -The Django REST framework offers tools that make building APIs easier and quicker, including classes and methods specifically designed for handling API requests and responses.

  • How can one get started with the Django REST framework?

    -To get started with the Django REST framework, one can install it using pip, create a virtual environment, and then integrate it into an existing Django project.

  • What is the purpose of the 'response' import from the Django REST framework?

    -The 'response' import from the Django REST framework is used to return responses from API views, similar to Django's built-in JSON response but with additional functionality provided by the framework.

Outlines

00:00

🚀 Introduction to APIs and Django REST Framework

This paragraph introduces the topic of the video series, which focuses on building APIs with Django REST Framework. It emphasizes that the video will provide an introduction to what APIs are and how Django can be used to build them. The speaker suggests that viewers who are new to Django might find the concepts challenging and recommends starting with understanding Django basics. They also mention courses on the channel and provide links in the video description for further learning. The speaker then explains that viewers familiar with Django concepts like views, URLs, templates, and forms should have no problem understanding the Django REST Framework. The paragraph ends with a promotion for 'Just Django', a resource for Django developers, and a teaser for the upcoming content about APIs.

05:01

📚 Understanding APIs and Their Purpose

The speaker delves into the concept of APIs (Application Programming Interfaces), breaking down the term into 'application', 'programming', and 'interface' to clarify what APIs are. They explain that APIs facilitate communication between processes by sending and receiving messages. The paragraph discusses the reasons for using or building APIs, such as sharing applications, integrating services, or having multiple services communicate with a central service. An example is given where a website built with React and a mobile app built with Flutter both query data from a central API. The speaker also contrasts APIs with traditional web pages, explaining that APIs return data in a JSON format, which can be used flexibly within various applications.

10:01

🛠️ Setting Up a Django Project for API Development

The video script outlines the steps for setting up a Django project to develop an API. It starts with creating a virtual environment using 'virtualenv' and installing Django. The process of creating a Django project named 'DRF_API' is detailed, along with renaming the top-level folder to 'source'. The creation of an app called 'core' within the project is also covered. The speaker then demonstrates how to create a basic view that returns a JSON response, using Django's HTTP module to import 'JSONResponse'. The view is linked to a URL, and the speaker shows how to start the server and access the JSON response through a web browser. The paragraph concludes with a demonstration of using 'curl' to retrieve the JSON data from the API endpoint.

15:04

🌐 Exploring Django REST Framework and API Views

In this paragraph, the focus shifts to using Django REST Framework for building APIs. The script describes the process of installing the framework and creating a class-based view for an API. It explains how to import necessary modules from the framework and create an API view class that inherits from 'APIView'. The class-based view allows handling different HTTP methods like GET and POST. The speaker demonstrates updating the view to return a JSON response using the 'Response' class from the framework. The video script also shows how to update the project's settings to include the Django REST Framework as an installed app and modify the URL configuration to use the new API view. The paragraph concludes with a discussion of the benefits of using Django REST Framework, such as the admin interface for APIs and the potential for future exploration of more advanced features in the series.

🔍 Conclusion and Preview of Future Content

The final paragraph summarizes the overview of Django REST Framework provided in the video. It mentions that the video has only scratched the surface of the framework's capabilities and that more in-depth exploration will be covered in subsequent videos. The speaker thanks the viewers for watching, encourages them to subscribe, and hints at what will be covered in the next video of the series.

Mindmap

Keywords

💡API

An API, or Application Programming Interface, is a set of protocols that allows different software applications to communicate with each other. In the video, it is explained as an interface that enables the exchange of data between two processes, such as a server and a front-end application (e.g., a website or mobile app). The video focuses on building APIs using the Django REST framework.

💡Django REST Framework

The Django REST Framework (DRF) is a powerful toolkit for building Web APIs using Django. In the video, DRF is presented as a way to simplify the process of building APIs, making it faster and easier by offering pre-built tools for handling requests, responses, and other common tasks. DRF supports authentication, permissions, and more, allowing developers to focus on building API functionality.

💡JSON

JSON (JavaScript Object Notation) is a lightweight data format used for exchanging data between a server and a client. In the video, the instructor highlights that APIs typically return data in JSON format. For example, a Django view may return a JSON payload containing keys and values that can be rendered by a front-end application, such as a React app.

💡HTTP Methods

HTTP methods such as GET, POST, PUT, and DELETE define the type of operation to be performed by the API. In the video, the instructor explains how the Django REST Framework uses these methods to handle different types of requests, such as retrieving data (GET) or submitting new data (POST). These methods allow the API to manage resources effectively.

💡Views

In Django, a 'view' handles the logic behind an endpoint. The video explains how both function-based and class-based views can be used to return data, particularly through JSON responses. With the Django REST Framework, API views are extended to handle HTTP requests and provide a structured way to manage and respond to those requests.

💡Serializer

A serializer is a Django REST Framework component that converts complex data types, such as Django querysets and model instances, into JSON format, and vice versa. The video mentions that serializers will be discussed in later parts of the series, as they are essential for transforming database objects into JSON data that can be consumed by front-end applications.

💡Virtual Environment

A virtual environment is an isolated Python environment that helps manage dependencies for a project. In the video, the instructor demonstrates setting up a virtual environment using 'virtualenv' and activating it before installing Django and other necessary packages, ensuring that the project’s dependencies do not interfere with system-wide packages.

💡JSONResponse

JSONResponse is a Django utility that allows views to return data as JSON objects. In the video, the instructor uses JSONResponse to send a simple dictionary as a JSON response from a view. This function is crucial for creating APIs because APIs primarily handle data, not HTML, making JSONResponse the ideal return format for API data.

💡REST

REST, or Representational State Transfer, is an architectural style for designing networked applications. The video touches on REST principles, explaining that a REST API focuses on data exchange between clients and servers, typically using standard HTTP methods. RESTful APIs are stateless, meaning each request from a client must contain all necessary information.

💡Authentication

Authentication refers to the process of verifying the identity of a user or service interacting with an API. Although only briefly mentioned in this introductory video, the series promises to cover topics like authentication, where users need to prove their identity to access or modify certain resources via the API, ensuring that only authorized users can perform specific actions.

Highlights

Introduction to APIs and how to use Django to build them.

Suggestion for beginners to understand Django before diving into APIs.

Link to courses on the channel for learning Django.

Explanation of Django views, URLs, templates, and forms as prerequisites.

Promotion of 'Just Django' for structured learning of Django.

Definition of an API as an application programming interface.

Explanation of the purpose of an API as a communication interface.

Reasons for using or building an API, such as sharing applications or integrating services.

Description of an API's role in providing data, typically in JSON format.

Overview of the series covering REST API principles and Django REST framework.

Instructions on setting up a virtual environment for Django projects.

Step-by-step guide to creating a Django project and app.

Creation of a basic view that returns a JSON response.

Explanation of how to link the view to a URL in Django.

Demonstration of testing the API endpoint using a web browser and curl.

Introduction to the Django REST framework and its benefits over standard Django.

Installation instructions for the Django REST framework.

Transformation of a function view into a class-based view using the APIView class.

Integration of the Django REST framework views and responses.

Final demonstration of the API endpoint with the Django REST framework.

Teaser for upcoming videos covering authentication, permissions, serializers, and API views.

Transcripts

play00:00

hi and welcome to a video part of a new

play00:02

series we will be talking about api's

play00:04

and particularly building api's with the

play00:07

Django rest framework so in this video

play00:09

we're just going to go into an

play00:11

introduction of what api's are and how

play00:13

you can use django to build APRs so if

play00:17

you're new to django then these concepts

play00:19

will probably be a little bit difficult

play00:21

for you so I suggest that you rather

play00:23

start off with understanding Django

play00:25

itself there are some courses on this

play00:27

channel that you can watch and we'll

play00:29

provide links in the description of this

play00:31

video so that you can check those ones

play00:32

out but other than that if you're

play00:34

familiar with Django views URLs

play00:36

templates and mainly forms just how to

play00:39

accept data and create instances in the

play00:42

database then you shouldn't have a

play00:44

problem with understanding how the

play00:45

Django rest framework works so with that

play00:48

let's get started everyone so before

play01:02

getting into it if you're interested in

play01:03

becoming a better Django developer then

play01:05

check out just Django com the link is in

play01:07

the description of this video and here

play01:08

you can find a bunch of different

play01:10

courses and mainly a structured roadmap

play01:13

on how to learn Django right now there's

play01:15

over 25 hours of content for learning

play01:18

just Django and here you'll be able to

play01:20

start at the absolute basics and work

play01:22

your way through more difficult concepts

play01:25

in an incremental way all the way to

play01:27

much more advanced applications and

play01:29

ultimately becoming a better Django

play01:32

developer so if you're interested in

play01:33

that do check out the link below we do

play01:35

have a special at the moment and as a

play01:37

right now there are only 32 places left

play01:39

but otherwise enjoy this one all right

play01:42

so the first question is what is an API

play01:45

now an API stands for application

play01:48

programming interface and when you

play01:53

explain it like this it's a little bit

play01:54

difficult to understand what that

play01:55

actually means so when we go into each

play01:58

of these three words it'll start to make

play02:00

a bit more sense so first it's an

play02:02

application so it's an actual app

play02:04

something that is functional we're

play02:07

programming it so there isn't a

play02:09

programming aspect of it and then it's

play02:11

most importantly

play02:13

interface an interface is something that

play02:15

allows two things to communicate to each

play02:18

other and that's basically what sums up

play02:20

an API it's something that allows two

play02:23

processes to communicate with each other

play02:24

and the way that you achieve that the

play02:27

way that you allow these processes to

play02:29

communicate is by programming an

play02:31

application that allows them to then

play02:33

send and receive messages now the next

play02:37

question is why use or slash build an

play02:40

API and this kind of follows on from the

play02:44

interfacing pot maybe you have some sort

play02:47

of application that you'd like to share

play02:49

with others you'd like other people to

play02:51

integrate with and use in their services

play02:53

or you just have multiple services that

play02:57

need to communicate with one central

play02:58

service so for example if you have a

play03:01

website that's built with an API and

play03:03

it's just a REST API on a server then

play03:06

you've got your website which is built

play03:08

with let's say react and it's a static

play03:10

website and then you've got a mobile

play03:12

application bolt with flutter but you

play03:15

have one central API that all of those

play03:17

front-ends are querying data from well

play03:19

in that case your API is kind of like a

play03:21

central service and everything else can

play03:24

communicate with that central service to

play03:26

get the data and work with the data in

play03:28

their own unique ways so the why of

play03:31

using or building an API is depending on

play03:34

your goal and the application you're

play03:36

building but it's mainly just to allow

play03:38

things to communicate and integrate now

play03:41

the whole point of an API is that it

play03:44

only provides the data so if we look at

play03:47

normal websites where you go to let's

play03:49

say Wikipedia comm you open up the

play03:52

browser and you go to that page it's

play03:54

going to render the HTML the CSS the

play03:56

JavaScript the images all of that is

play03:58

going to make up the web page that you

play03:59

see now in that case the response that

play04:02

you get back from the server could be

play04:04

some text some HTML some JavaScript the

play04:07

actual file that you're querying we as

play04:10

an API is returning you only the data

play04:12

that you want to render so that data is

play04:15

typically returned in a JSON response so

play04:19

that means it's a kind of like

play04:21

dictionary and it has keys and values

play04:23

and that payload

play04:26

some people call it is what you will

play04:28

then work with in your own application

play04:31

however you want to so the most

play04:34

important thing about an API is that it

play04:36

is only returning you the data and

play04:38

because API is only send and receive

play04:41

data that's what allows you to integrate

play04:44

with multiple services because that's

play04:46

all you really care about is getting the

play04:48

data or submitting data to that service

play04:51

and so that's basically a summary of

play04:53

what an API is why we use api's and how

play04:57

they work and now we can take a look at

play05:00

how we can actually implement this sort

play05:02

of logic using Django and then the

play05:05

Django rest framework so just a brief

play05:07

overview of what this series is going to

play05:10

cover in this video we're going to be

play05:12

dealing with the REST API principles

play05:14

look at the Django rest framework

play05:15

towards the end and in further videos

play05:18

we'll cover more about the Django rest

play05:20

framework and topics such as

play05:22

authentication permissions serializers

play05:24

building API views and then later on

play05:27

looking at how we can integrate that

play05:29

with front-ends or mobile applications

play05:32

like react view and flutter and so with

play05:35

that we can then go and create a Django

play05:36

project so first we're going to create

play05:39

our virtual environment you'll need

play05:41

virtual invi installed so you can just

play05:43

say pip install virtual envy and then

play05:45

you can just create a virtual

play05:46

environment like this once you have it

play05:48

you can inactivate it with source and

play05:50

we've been activate that's if you're on

play05:52

Mac or Linux otherwise it should be

play05:55

something like env scripts activate if

play05:59

you're on Windows but now that it's

play06:02

activated we can go and say pip install

play06:04

Django that'll get us the latest version

play06:07

which is now two point two point six and

play06:10

once we have that we can then say Django

play06:11

- admin start project and we'll just

play06:15

call it

play06:15

DRF API and okay no dashes so let's just

play06:20

make that do F underscore API alright so

play06:23

there's our folder there and there's our

play06:27

kind of route which has the settings

play06:29

URLs and WSGI so the top-level folder

play06:32

I'm just going to rename to source and

play06:34

then we can go and create some apps

play06:36

inside there and work with our Django

play06:39

project inside

play06:40

sauce folder so what I'm going to do is

play06:42

just say Python managed the PI and let's

play06:45

actually just change into the source

play06:47

directory so we'll say manage top I

play06:50

start app and we'll just call it let's

play06:53

say core so inside core we now have our

play06:57

typical app structure what I'm going to

play06:59

do is just go into views and here we're

play07:02

just going to create a basic view which

play07:04

will return a JSON response so this is

play07:07

just going to be a function will just

play07:08

say to find test view it'll just take a

play07:12

request and normally if we wanted to

play07:14

return some HTML with some context

play07:17

inside it then we would say return

play07:19

render and pass in the request but

play07:22

because we don't want to return HTML we

play07:24

don't want to return text per se we want

play07:26

to return only the data which is a JSON

play07:29

payload so what we do is we make use of

play07:32

the JSON response this is something

play07:34

built into Django's HTTP module so we

play07:37

can say from Django dot HTTP import JSON

play07:44

response we also get a bunch of other

play07:46

ones like HTTP response and you can see

play07:49

all of these over here so this is your

play07:51

typical HTTP response which will allow

play07:53

you to return some HTML or just normal

play07:56

text but again we're interested in only

play07:58

the data so we return the JSON response

play08:01

so what I'm going to do is just build a

play08:03

data dictionary here and we could say

play08:06

name and we can just say John and we'll

play08:12

say age and let's say 23 then we can

play08:18

just say return JSON response of that

play08:20

data

play08:21

notice that the data is a dictionary so

play08:24

JSON response by default is accepting a

play08:28

dictionary however if you do want to

play08:30

pass in other data types such as a list

play08:32

then what you can do is you can pass in

play08:34

the safe argument and just set that to

play08:37

false that'll allow you to pass in

play08:40

things like lists but because we have an

play08:42

a dictionary that we're working with

play08:44

this is fine so now we can link this up

play08:46

to a URL and we can just do this in our

play08:49

root URL configuration so we'll just go

play08:52

in here and we can

play08:54

from cord views import that test view

play08:59

and then we can go to path let's just

play09:02

make it the empty path and we'll say

play09:04

test view and we could just say name

play09:06

this test okay so now we can just save

play09:10

manage that pi migrate so we get those

play09:14

initial migrations and then we can say

play09:17

run the server alright there it is so

play09:22

now we can go and open that up and

play09:23

immediately you see we're on the kind of

play09:26

root URL and we get this JSON payload

play09:29

that's returned to us even though it's

play09:31

being displayed in the browser event

play09:33

looks like HTML it's not this is

play09:35

actually Jason what we can do is

play09:38

actually inspect this page and if you go

play09:41

to your network and then just refresh

play09:43

the page again then you'll see that you

play09:45

have one response which is from our

play09:48

local host and here's all the request

play09:52

information if we go to the response you

play09:55

can see that this is the data that was

play09:56

returned and just to prove that this is

play09:59

JSON what we can do is come in here and

play10:01

open up another terminal and we can use

play10:03

curl which is something that we have on

play10:05

Linux and Mac and we can just say HTTP

play10:09

and we can go to our local host witches

play10:13

127 port 8000 and there you can see we

play10:17

get that data being returned to us so

play10:20

this is what the JSON response does it's

play10:23

returning that data as a JSON payload

play10:26

and this is kind of the beginning of

play10:28

what an API is because if we had some

play10:31

sort of model let's say we were working

play10:33

with posts then we would have a post

play10:35

list view and we would return all of our

play10:37

posts in a JSON payload where each post

play10:40

would be at its own dictionary and it

play10:43

would have all of the fields of that

play10:45

model populated inside here so maybe

play10:47

your post has a title it has a

play10:48

description etc all of that will be

play10:51

represented as a dictionary and then

play10:53

converted into a JSON payload and the

play10:56

same thing for retrieving an instance so

play10:59

if you have a detail view the same thing

play11:01

for creating a post if you have a create

play11:03

view you'll have to submit data you

play11:05

won't be just submitting

play11:07

request stop post and the same thing for

play11:10

an update view and a delete view and so

play11:12

this is where the Django rest framework

play11:14

comes in it basically allows you to

play11:16

build an API a lot easier and a lot

play11:19

quicker than what you can do with just

play11:22

normal Django so to get started with

play11:24

that we can actually just come here and

play11:26

search for the Django rest framework and

play11:29

there it is first link on Google so this

play11:32

is their home page we can go to

play11:34

installation and it's just pup install

play11:38

Django rest framework so we're just

play11:40

gonna go with that come here and stop

play11:43

the server install that and what we can

play11:46

do is they put freeze into a

play11:49

requirements of txt file okay so there's

play11:54

our requirements and so now if we come

play11:58

back to this view here we can then come

play12:00

here and we'll say these are third-party

play12:02

imports and we can say from rest

play12:05

framework now we have that package

play12:07

installed we can say dot views import

play12:13

the API view this is one of their

play12:16

wrappers that allows you to create an

play12:18

API view that accepts certain request

play12:21

methods so being either a get request or

play12:24

post request and all the other types of

play12:26

methods and then we're also going to

play12:29

import the response from rest framework

play12:32

dot response so we imported like this

play12:37

and this is basically what you will

play12:39

return it's basically inheriting from

play12:41

the JSON response that we see here built

play12:44

in with Django so what I'm going to do

play12:45

is just comment that out and then what

play12:48

we can do is we'll run the server as

play12:50

well but this time instead of creating a

play12:53

function we're going to create a class

play12:55

because the API view is something that

play12:58

we can inherit in our own classes so

play13:02

we'll just say this is our test view and

play13:04

it's going to inherit from that API view

play13:07

which gives us a lot of methods that we

play13:09

can work with and one of them is the get

play13:13

method which is just going to take in

play13:15

self request args and keyword Oggs

play13:20

and this is very

play13:21

similar to just a normal Django

play13:23

class-based view so this get method is

play13:26

what we'll use to handle when someone

play13:28

sends a get request to this endpoint and

play13:31

we'll use the response to return a

play13:34

response at the end of this request so

play13:38

we can actually go and get that exact

play13:40

same payload that we have here and we'll

play13:43

just paste it there then we can say

play13:45

return a response of that data we can

play13:52

take this test view into the URLs and

play13:54

actually just get rid of that one there

play13:56

so we have to say test view dot as view

play13:59

because it's a class space to you and

play14:02

with that we can then come back here and

play14:04

oh okay we just need to address

play14:07

framework as an installed app so let's

play14:09

just do that in the settings so just add

play14:12

it over here and okay and one more URL

play14:19

pattern in the URL configuration you can

play14:24

also just change this into a path we can

play14:27

get rid of that and just like that and

play14:30

we'll import include and let's just make

play14:36

sure here we're not going to copy the

play14:38

rest of em exciting's in just yet so if

play14:40

we just come back here and refresh this

play14:42

now you can see we're getting a little

play14:44

bit of an admin kind of interface with

play14:47

our data so we can see it was an HTTP

play14:50

200 response that we got back as the

play14:53

status code and that means it's

play14:55

basically a successful request this

play14:58

endpoint allows the get request and

play15:00

that's because we've specified the get

play15:03

method on this API view and the content

play15:07

type is application JSON so that means

play15:09

that it's going to return a JSON payload

play15:11

as the actual content type of this

play15:15

response and then we have the actual

play15:18

data that was returned which is what

play15:20

we've been working with so far and this

play15:22

is basically what the Django rest

play15:24

framework provides you with a little

play15:25

admin interface to work with your API a

play15:28

little bit better but what you can do is

play15:30

also come here and change this to JSON

play15:33

and there you'll see that now

play15:35

it's just giving us that raw format so

play15:38

here you can see it's in the query

play15:40

parameters of this URL but if we just

play15:43

come back here we can also get a lot

play15:45

more information about it

play15:47

so this is just a little bit of an

play15:48

overview as to what the Django rest

play15:50

framework provides we've only just

play15:52

touched the surface of these modules and

play15:56

everything that you can use from the

play15:58

rest framework package and we'll

play16:00

definitely be exploring more of it as we

play16:01

get into the series so thanks for

play16:03

watching don't forget to subscribe and

play16:05

we'll see you in the next one

play16:11

[Music]

Rate This

5.0 / 5 (0 votes)

相关标签
API DevelopmentDjango RESTWeb DevelopmentEducational SeriesJSON ResponsesAPI IntegrationDjango TutorialWeb APIsDeveloper GuidesRESTful Services
您是否需要英文摘要?