WEBVTT
Kind: captions
Language: en

00:00:00.060 --> 00:00:06.240
Now that we've created a serializer for
our app we can go ahead and add the post

00:00:06.240 --> 00:00:10.000
functionality to our API view

00:00:10.000 --> 00:00:13.440
Start by
opening up the views dot py file and

00:00:13.440 --> 00:00:18.660
we're going to add a couple more imports to
the top of the file so the first one

00:00:18.660 --> 00:00:23.220
we're going to add is the status import
from the rest framework so let's type

00:00:23.220 --> 00:00:31.759
from rest framework import status the
second input we're going to add is the

00:00:31.759 --> 00:00:37.649
serializers module that we created in
the previous video so type from profiles

00:00:37.649 --> 00:00:42.840
API import serializers

00:00:42.840 --> 00:00:47.430
The status object
from the rest framework is a list of

00:00:47.430 --> 00:00:53.399
handy HTTP status codes that you can use
when returning responses from your API

00:00:53.399 --> 00:00:59.280
so we're going to be using these status
codes in our post in our post function

00:00:59.280 --> 00:01:01.760
handler

00:01:01.760 --> 00:01:05.519
These serializers is the
serializer module that we created in our

00:01:05.519 --> 00:01:11.490
profiles API project we're going to use
this to tell our API view what data to

00:01:11.490 --> 00:01:19.049
expect when making post put and patch
requests to our API so we set the

00:01:19.049 --> 00:01:26.490
sterilizer by typing serializer
underscore class equals serializers dot

00:01:26.490 --> 00:01:29.880
hello serializer

00:01:29.880 --> 00:01:33.960
This configures our API
view to have the serializer class that

00:01:33.960 --> 00:01:40.079
we created in the previous video so this
says whenever you're making a or

00:01:40.079 --> 00:01:45.930
whenever you're sending a post put or
patch request expect an input with name

00:01:45.930 --> 00:01:51.880
and we're going to validate that input
to a maximum length of 10

00:01:51.880 --> 00:01:52.820
Okay so now we

00:01:52.829 --> 00:01:59.820
can add the post function to our API
view create a new function under the get

00:01:59.820 --> 00:02:10.470
function that we created previously and
type def post self comma request and then

00:02:10.470 --> 00:02:17.470
we'll add the doc string create a hello
message with our name and this is what

00:02:17.470 --> 00:02:23.260
we're going to do when we receive a post
request to our hello API

00:02:23.260 --> 00:02:24.240
So what we'll

00:02:24.250 --> 00:02:29.890
do first is we'll retrieve the
serializer and pass in the data that was

00:02:29.890 --> 00:02:37.600
sent in the request so type serializer
equals self dot serializer

00:02:37.600 --> 00:02:45.800
underscore class and then data equals
request dot data

00:02:45.800 --> 00:02:47.200
The self dot

00:02:47.200 --> 00:02:51.970
serializer class function is a function
that comes with the API view that

00:02:51.970 --> 00:02:58.300
retrieves the configured serializer
class for our view so it's the

00:02:58.300 --> 00:03:02.440
standard way that you should retrieve
the serializer class when working with

00:03:02.440 --> 00:03:05.140
serializers in a view

00:03:05.140 --> 00:03:09.190
The second part
assigns the data so when you make a post

00:03:09.190 --> 00:03:17.470
request to our API view the data gets
passed in as request dot data so it is

00:03:17.470 --> 00:03:23.470
part of the request object that's passed
to our post request we assign this data

00:03:23.470 --> 00:03:28.450
to our serializer class and then we
create a new variable for our serializer

00:03:28.450 --> 00:03:30.740
class called serializer

00:03:30.740 --> 00:03:33.940
So next we can
go ahead and validate this serializer

00:03:33.940 --> 00:03:39.040
so I mentioned previously that the
Django rest framework serializers

00:03:39.040 --> 00:03:43.750
provide the functionality to validate
the input so that is ensuring that the

00:03:43.750 --> 00:03:50.590
input is valid as per the specification
of our serializer fields in our case

00:03:50.590 --> 00:03:58.209
we're going to be validating that the
name is no longer than 10 characters the

00:03:58.209 --> 00:04:04.120
way you validate a serializer is you
call it is valid class so you can type

00:04:04.120 --> 00:04:12.160
if serializer dot is underscore valid
and then if it is valid we'll perform the

00:04:12.160 --> 00:04:16.330
logic that we want to do if the
serializer is valid so what we want to do

00:04:16.330 --> 00:04:23.020
is retrieve the name field from the
validated data so type name equals

00:04:23.020 --> 00:04:30.100
serializer dot validated data
dot get name

00:04:30.100 --> 00:04:31.440
this retrieves the name

00:04:31.450 --> 00:04:35.380
field that we defined and you can
retrieve any field that you define in

00:04:35.380 --> 00:04:37.880
your serializer this way

00:04:37.880 --> 00:04:40.780
Okay so now
that we have it we're going to create a

00:04:40.780 --> 00:04:45.430
new message and this message we're just
going to return a message from our API

00:04:45.430 --> 00:04:49.540
that contains the name that was passed
in the post request just to demonstrate

00:04:49.540 --> 00:04:54.330
how the post request works so let's
create a message variable called message

00:04:54.330 --> 00:05:00.280
equals and we're going to use the F
string functionality here to insert the

00:05:00.280 --> 00:05:05.500
name into a string so we'll type a
string that says hello and then the way

00:05:05.500 --> 00:05:10.780
you insert variables into a string if
you put this F before the string you can

00:05:10.780 --> 00:05:15.820
simply use the braces like this to
insert a variable from your code into

00:05:15.820 --> 00:05:20.560
the string so we're going to insert the
name variable

00:05:20.560 --> 00:05:21.940
Okay then after we have

00:05:21.940 --> 00:05:27.190
this variable here message we're going
to return this as a response so type

00:05:27.190 --> 00:05:33.760
return response and then pass in a
dictionary to the response and we're

00:05:33.760 --> 00:05:39.930
going to give it the key message and then
we're going to pass in the message variable

00:05:39.930 --> 00:05:45.640
okay that's all good if the input is
valid but what should happen if the

00:05:45.640 --> 00:05:52.820
input is not valid well we're going to
return a HTTP 400 bad request

00:05:52.820 --> 00:06:00.640
response so in the if
statement type else : and then return

00:06:00.640 --> 00:06:07.450
and return a response and then in the
response we're going to pass in the

00:06:07.450 --> 00:06:14.140
errors that were generated by the
serializer so if you type serializer dot

00:06:14.140 --> 00:06:19.330
errors this will give you a dictionary
of all the errors based on the

00:06:19.330 --> 00:06:24.970
validation rules that were applied to
the serializer so it's a good idea to

00:06:24.970 --> 00:06:30.610
return this so the person that's using
the API knows what went wrong when they

00:06:30.610 --> 00:06:35.460
tried to submit an invalid response

00:06:35.460 --> 00:06:40.150
Okay
by default the response returns HTTP 200

00:06:40.150 --> 00:06:46.389
okay request now since this was an error
we need to change this to a 400 bad

00:06:46.389 --> 00:06:51.580
request so we return the standard error
response code for this type of error in

00:06:51.580 --> 00:06:56.860
an API so let's actually
break this down on to separate lines

00:06:56.860 --> 00:07:05.560
here and let's add a second parameter
called status equals status dot and then

00:07:05.560 --> 00:07:12.020
in all capitals HTTP 400 bad request

00:07:12.020 --> 00:07:14.889
Now
you could pass in just an integer 400

00:07:14.889 --> 00:07:18.910
here however it's good to use this
status object here to get it because

00:07:18.910 --> 00:07:23.800
then you can easily see what the request
means when you're looking at the code so

00:07:23.800 --> 00:07:30.270
we know this 400 bad request means there
was a bad request made to our API

00:07:30.270 --> 00:07:36.090
okay so save that file and that's how
you add the post functionality to your

00:07:36.090 --> 00:07:39.539
API view

