Now that we've created a serializer for our app we can go ahead and add the post
functionality to our API view
Start by opening up the views dot py file and
we're going to add a couple more imports to the top of the file so the first one
we're going to add is the status import from the rest framework so let's type
from rest framework import status the second input we're going to add is the
serializers module that we created in the previous video so type from profiles
API import serializers
The status object from the rest framework is a list of
handy HTTP status codes that you can use when returning responses from your API
so we're going to be using these status codes in our post in our post function
handler
These serializers is the serializer module that we created in our
profiles API project we're going to use this to tell our API view what data to
expect when making post put and patch requests to our API so we set the
sterilizer by typing serializer underscore class equals serializers dot
hello serializer
This configures our API view to have the serializer class that
we created in the previous video so this says whenever you're making a or
whenever you're sending a post put or patch request expect an input with name
and we're going to validate that input to a maximum length of 10
Okay so now we
can add the post function to our API view create a new function under the get
function that we created previously and type def post self comma request and then
we'll add the doc string create a hello message with our name and this is what
we're going to do when we receive a post request to our hello API
So what we'll
do first is we'll retrieve the serializer and pass in the data that was
sent in the request so type serializer equals self dot serializer
underscore class and then data equals request dot data
The self dot
serializer class function is a function that comes with the API view that
retrieves the configured serializer class for our view so it's the
standard way that you should retrieve the serializer class when working with
serializers in a view
The second part assigns the data so when you make a post
request to our API view the data gets passed in as request dot data so it is
part of the request object that's passed to our post request we assign this data
to our serializer class and then we create a new variable for our serializer
class called serializer
So next we can go ahead and validate this serializer
so I mentioned previously that the Django rest framework serializers
provide the functionality to validate the input so that is ensuring that the
input is valid as per the specification of our serializer fields in our case
we're going to be validating that the name is no longer than 10 characters the
way you validate a serializer is you call it is valid class so you can type
if serializer dot is underscore valid and then if it is valid we'll perform the
logic that we want to do if the serializer is valid so what we want to do
is retrieve the name field from the validated data so type name equals
serializer dot validated data dot get name
this retrieves the name
field that we defined and you can retrieve any field that you define in
your serializer this way
Okay so now that we have it we're going to create a
new message and this message we're just going to return a message from our API
that contains the name that was passed in the post request just to demonstrate
how the post request works so let's create a message variable called message
equals and we're going to use the F string functionality here to insert the
name into a string so we'll type a string that says hello and then the way
you insert variables into a string if you put this F before the string you can
simply use the braces like this to insert a variable from your code into
the string so we're going to insert the name variable
Okay then after we have
this variable here message we're going to return this as a response so type
return response and then pass in a dictionary to the response and we're
going to give it the key message and then we're going to pass in the message variable
okay that's all good if the input is valid but what should happen if the
input is not valid well we're going to return a HTTP 400 bad request
response so in the if statement type else : and then return
and return a response and then in the response we're going to pass in the
errors that were generated by the serializer so if you type serializer dot
errors this will give you a dictionary of all the errors based on the
validation rules that were applied to the serializer so it's a good idea to
return this so the person that's using the API knows what went wrong when they
tried to submit an invalid response
Okay by default the response returns HTTP 200
okay request now since this was an error we need to change this to a 400 bad
request so we return the standard error response code for this type of error in
an API so let's actually break this down on to separate lines
here and let's add a second parameter called status equals status dot and then
in all capitals HTTP 400 bad request
Now you could pass in just an integer 400
here however it's good to use this status object here to get it because
then you can easily see what the request means when you're looking at the code so
we know this 400 bad request means there was a bad request made to our API
okay so save that file and that's how you add the post functionality to your
API view