Build an API with Django // Part 1 - What is an API?
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
đ 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.
đ 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.
đ ïž 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.
đ 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
đĄDjango REST Framework
đĄJSON
đĄHTTP Methods
đĄViews
đĄSerializer
đĄVirtual Environment
đĄJSONResponse
đĄREST
đĄAuthentication
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
hi and welcome to a video part of a new
series we will be talking about api's
and particularly building api's with the
Django rest framework so in this video
we're just going to go into an
introduction of what api's are and how
you can use django to build APRs so if
you're new to django then these concepts
will probably be a little bit difficult
for you so I suggest that you rather
start off with understanding Django
itself there are some courses on this
channel that you can watch and we'll
provide links in the description of this
video so that you can check those ones
out but other than that if you're
familiar with Django views URLs
templates and mainly forms just how to
accept data and create instances in the
database then you shouldn't have a
problem with understanding how the
Django rest framework works so with that
let's get started everyone so before
getting into it if you're interested in
becoming a better Django developer then
check out just Django com the link is in
the description of this video and here
you can find a bunch of different
courses and mainly a structured roadmap
on how to learn Django right now there's
over 25 hours of content for learning
just Django and here you'll be able to
start at the absolute basics and work
your way through more difficult concepts
in an incremental way all the way to
much more advanced applications and
ultimately becoming a better Django
developer so if you're interested in
that do check out the link below we do
have a special at the moment and as a
right now there are only 32 places left
but otherwise enjoy this one all right
so the first question is what is an API
now an API stands for application
programming interface and when you
explain it like this it's a little bit
difficult to understand what that
actually means so when we go into each
of these three words it'll start to make
a bit more sense so first it's an
application so it's an actual app
something that is functional we're
programming it so there isn't a
programming aspect of it and then it's
most importantly
interface an interface is something that
allows two things to communicate to each
other and that's basically what sums up
an API it's something that allows two
processes to communicate with each other
and the way that you achieve that the
way that you allow these processes to
communicate is by programming an
application that allows them to then
send and receive messages now the next
question is why use or slash build an
API and this kind of follows on from the
interfacing pot maybe you have some sort
of application that you'd like to share
with others you'd like other people to
integrate with and use in their services
or you just have multiple services that
need to communicate with one central
service so for example if you have a
website that's built with an API and
it's just a REST API on a server then
you've got your website which is built
with let's say react and it's a static
website and then you've got a mobile
application bolt with flutter but you
have one central API that all of those
front-ends are querying data from well
in that case your API is kind of like a
central service and everything else can
communicate with that central service to
get the data and work with the data in
their own unique ways so the why of
using or building an API is depending on
your goal and the application you're
building but it's mainly just to allow
things to communicate and integrate now
the whole point of an API is that it
only provides the data so if we look at
normal websites where you go to let's
say Wikipedia comm you open up the
browser and you go to that page it's
going to render the HTML the CSS the
JavaScript the images all of that is
going to make up the web page that you
see now in that case the response that
you get back from the server could be
some text some HTML some JavaScript the
actual file that you're querying we as
an API is returning you only the data
that you want to render so that data is
typically returned in a JSON response so
that means it's a kind of like
dictionary and it has keys and values
and that payload
some people call it is what you will
then work with in your own application
however you want to so the most
important thing about an API is that it
is only returning you the data and
because API is only send and receive
data that's what allows you to integrate
with multiple services because that's
all you really care about is getting the
data or submitting data to that service
and so that's basically a summary of
what an API is why we use api's and how
they work and now we can take a look at
how we can actually implement this sort
of logic using Django and then the
Django rest framework so just a brief
overview of what this series is going to
cover in this video we're going to be
dealing with the REST API principles
look at the Django rest framework
towards the end and in further videos
we'll cover more about the Django rest
framework and topics such as
authentication permissions serializers
building API views and then later on
looking at how we can integrate that
with front-ends or mobile applications
like react view and flutter and so with
that we can then go and create a Django
project so first we're going to create
our virtual environment you'll need
virtual invi installed so you can just
say pip install virtual envy and then
you can just create a virtual
environment like this once you have it
you can inactivate it with source and
we've been activate that's if you're on
Mac or Linux otherwise it should be
something like env scripts activate if
you're on Windows but now that it's
activated we can go and say pip install
Django that'll get us the latest version
which is now two point two point six and
once we have that we can then say Django
- admin start project and we'll just
call it
DRF API and okay no dashes so let's just
make that do F underscore API alright so
there's our folder there and there's our
kind of route which has the settings
URLs and WSGI so the top-level folder
I'm just going to rename to source and
then we can go and create some apps
inside there and work with our Django
project inside
sauce folder so what I'm going to do is
just say Python managed the PI and let's
actually just change into the source
directory so we'll say manage top I
start app and we'll just call it let's
say core so inside core we now have our
typical app structure what I'm going to
do is just go into views and here we're
just going to create a basic view which
will return a JSON response so this is
just going to be a function will just
say to find test view it'll just take a
request and normally if we wanted to
return some HTML with some context
inside it then we would say return
render and pass in the request but
because we don't want to return HTML we
don't want to return text per se we want
to return only the data which is a JSON
payload so what we do is we make use of
the JSON response this is something
built into Django's HTTP module so we
can say from Django dot HTTP import JSON
response we also get a bunch of other
ones like HTTP response and you can see
all of these over here so this is your
typical HTTP response which will allow
you to return some HTML or just normal
text but again we're interested in only
the data so we return the JSON response
so what I'm going to do is just build a
data dictionary here and we could say
name and we can just say John and we'll
say age and let's say 23 then we can
just say return JSON response of that
data
notice that the data is a dictionary so
JSON response by default is accepting a
dictionary however if you do want to
pass in other data types such as a list
then what you can do is you can pass in
the safe argument and just set that to
false that'll allow you to pass in
things like lists but because we have an
a dictionary that we're working with
this is fine so now we can link this up
to a URL and we can just do this in our
root URL configuration so we'll just go
in here and we can
from cord views import that test view
and then we can go to path let's just
make it the empty path and we'll say
test view and we could just say name
this test okay so now we can just save
manage that pi migrate so we get those
initial migrations and then we can say
run the server alright there it is so
now we can go and open that up and
immediately you see we're on the kind of
root URL and we get this JSON payload
that's returned to us even though it's
being displayed in the browser event
looks like HTML it's not this is
actually Jason what we can do is
actually inspect this page and if you go
to your network and then just refresh
the page again then you'll see that you
have one response which is from our
local host and here's all the request
information if we go to the response you
can see that this is the data that was
returned and just to prove that this is
JSON what we can do is come in here and
open up another terminal and we can use
curl which is something that we have on
Linux and Mac and we can just say HTTP
and we can go to our local host witches
127 port 8000 and there you can see we
get that data being returned to us so
this is what the JSON response does it's
returning that data as a JSON payload
and this is kind of the beginning of
what an API is because if we had some
sort of model let's say we were working
with posts then we would have a post
list view and we would return all of our
posts in a JSON payload where each post
would be at its own dictionary and it
would have all of the fields of that
model populated inside here so maybe
your post has a title it has a
description etc all of that will be
represented as a dictionary and then
converted into a JSON payload and the
same thing for retrieving an instance so
if you have a detail view the same thing
for creating a post if you have a create
view you'll have to submit data you
won't be just submitting
request stop post and the same thing for
an update view and a delete view and so
this is where the Django rest framework
comes in it basically allows you to
build an API a lot easier and a lot
quicker than what you can do with just
normal Django so to get started with
that we can actually just come here and
search for the Django rest framework and
there it is first link on Google so this
is their home page we can go to
installation and it's just pup install
Django rest framework so we're just
gonna go with that come here and stop
the server install that and what we can
do is they put freeze into a
requirements of txt file okay so there's
our requirements and so now if we come
back to this view here we can then come
here and we'll say these are third-party
imports and we can say from rest
framework now we have that package
installed we can say dot views import
the API view this is one of their
wrappers that allows you to create an
API view that accepts certain request
methods so being either a get request or
post request and all the other types of
methods and then we're also going to
import the response from rest framework
dot response so we imported like this
and this is basically what you will
return it's basically inheriting from
the JSON response that we see here built
in with Django so what I'm going to do
is just comment that out and then what
we can do is we'll run the server as
well but this time instead of creating a
function we're going to create a class
because the API view is something that
we can inherit in our own classes so
we'll just say this is our test view and
it's going to inherit from that API view
which gives us a lot of methods that we
can work with and one of them is the get
method which is just going to take in
self request args and keyword Oggs
and this is very
similar to just a normal Django
class-based view so this get method is
what we'll use to handle when someone
sends a get request to this endpoint and
we'll use the response to return a
response at the end of this request so
we can actually go and get that exact
same payload that we have here and we'll
just paste it there then we can say
return a response of that data we can
take this test view into the URLs and
actually just get rid of that one there
so we have to say test view dot as view
because it's a class space to you and
with that we can then come back here and
oh okay we just need to address
framework as an installed app so let's
just do that in the settings so just add
it over here and okay and one more URL
pattern in the URL configuration you can
also just change this into a path we can
get rid of that and just like that and
we'll import include and let's just make
sure here we're not going to copy the
rest of em exciting's in just yet so if
we just come back here and refresh this
now you can see we're getting a little
bit of an admin kind of interface with
our data so we can see it was an HTTP
200 response that we got back as the
status code and that means it's
basically a successful request this
endpoint allows the get request and
that's because we've specified the get
method on this API view and the content
type is application JSON so that means
that it's going to return a JSON payload
as the actual content type of this
response and then we have the actual
data that was returned which is what
we've been working with so far and this
is basically what the Django rest
framework provides you with a little
admin interface to work with your API a
little bit better but what you can do is
also come here and change this to JSON
and there you'll see that now
it's just giving us that raw format so
here you can see it's in the query
parameters of this URL but if we just
come back here we can also get a lot
more information about it
so this is just a little bit of an
overview as to what the Django rest
framework provides we've only just
touched the surface of these modules and
everything that you can use from the
rest framework package and we'll
definitely be exploring more of it as we
get into the series so thanks for
watching don't forget to subscribe and
we'll see you in the next one
[Music]
Voir Plus de Vidéos Connexes
5.0 / 5 (0 votes)