So in this lecture, let's add the ability for someone to be able to sign up for a user account via
our API in the first place, that will have to go is to the your Urals and say, you know what, your
l do we want someone to sign up with.
So let's say, you know, they've come to the API, you know, slash API.
What are we going to use?
I think let's keep it really simple.
Let's just use sign up.
So I'm going to say path and then I provide in here.
I want to sign up.
All right.
And then as far as what I'm pointing to.
I'm going to be using the views, but in this particular case, I'm going to be using a function based
view.
So I haven't created yet.
But the name that I'm going to use there is sign up.
So I'm going to use just the name sign up there and then I'll have my trailing karma.
All right.
So going to go ahead and save by you are else.
I got to copy the names, sign up here because this is what I'm going to be using inside of my views.
So let's go ahead and I'm going to do this up near the top here.
We can move down all our to do stuff and we've got to first make this function so I can do my def sign
up.
Which this is going to give us a request object.
OK, something that's really important with this is that we are going to be making it so that if someone
wants to sign up, that they can do it without it.
This we're looking for a post request from the user.
But we have to confirm that when they do this, you know that there's not going to be any issues with
the C.S. R.F., which is the cross site.
I think scripting.
I forget the acronym, but basically it usually protects Django requests from some malicious code that
users can use on sites.
But if it's an API, it's not something we have to worry about.
So in order to use this, we're gonna have to import the code.
So here we're gonna do an at C.
S r f underscore exempt.
OK.
Saying that we're not going to check it on this particular function here.
Now, in order to import this, we're going to have to say from and say with me, because this is a
bit of a big one, we're gonna do Django views, dot decorators.
Dot C.
S r f.
Import.
And then we want to import that C.
S, our F except J.
So now that we've got this in place and I've got to make sure that I always have spell inheres or DEC
or raid tours.
There we go.
So we have this code that says, OK, we'll be able to access this.
The next thing that we need to do is sort of check and make sure that this is what we're looking for.
And the great thing about having an existing project is we can reuse a lot of code.
So if we go to the views that pie of our to do app, we have a ton of code for if someone wants to sign
up a user account.
So I'm going to copy this huge chunk that we have here.
Okay.
Copy all of that.
I'm going to come back to my views dot pie, and I'm going to paste this in.
But we do need to change quite a bit here.
So the first one is we're looking for a post.
We only should accept post because if someone signing up, they're sending information, this should
be a post request.
So I'm gonna go ahead and get rid of the LDS because we're only interested to know if this is a post.
We don't have to check the passwords so we can go ahead and just delete this if statement and the whole
ELT's that comes at the bottom there, we can go ahead and just get rid of that.
And then let's go ahead and selected these lines.
I'm going to tab them in one space, at least in Adam.
If you do shift tab, you can go inside, whereas tab, you know, puts it to the right.
The other way gets you to the left.
Now with this, what we've got to do is we have to create a user object.
Now getting the proper information is not the exact same as before.
We want to use a Jason parser.
So what we're gonna do is import some code here that's going to get us this Jason parser.
So we're going to see from we want rest underscore framework, DOT parsers.
Import and then this is where we want all caps.
Jason pass, sir.
There we go.
Okay, so what we're gonna do is we're gonna get the information that is come back to us.
So we're going to say data is equal to this capital J.
Some passer.
Open and close parentheses dot pass.
And we want to to pass the request that has been sent in.
Okay, so now that we have this data, the data is essentially a dictionary to which we can get to anything
that we want.
So we're just going to take this data in rather than the request up post.
We'll just pass this in here as data and for the password, we'll have that be equal to the data and
go grab whatever the password is.
Now, because when someone's using this API, if someone's gonna be, you know, signing up for a user
account, we don't have to have password one and password to.
Like, if someone, for example, is making a mobile app of this to do project, they probably should
have the user enter in their password twice and confirm that they match.
But they can do that locally on the device.
And then when they go to create an account to sign up, they can just pass us the one password.
It's kind of silly to do that.
Checking here for us.
So I'm going to go ahead and just ask for simply a password.
We don't need password one.
But with that in place, we use all of the same code to be able to create a user.
And we still say user dot save.
That's all fine.
But rather than returning a redirect.
In this case, we want to send back a response and we want to send it via Jason.
Okay.
So you'll notice when we're doing the function based view, we don't get any of the magic that comes
with the class space views in the Django rest framework.
So it's a little bit unfortunate, although like this Jason parser, for example, this comes from the
jingle risk framework.
So there's some cool stuff there, but we kind of have to do a little bit more work when we're making
things custom.
So we're going to import some code to use a Jason response.
So I'm going to say from.
Django.
Spelled that correctly, Django Dot H.
TDP import.
Jay, some response and notice how this, Jason, is not all caps, but the one from the rest framework
is you got to be careful with, you know, which one you're looking at here.
But with this Jason response, we're gonna copy this and say so that when we do a return here, we're
gonna return the Jason response.
We're gonna inside of the parentheses here first pass forward a little dictionary with some information.
We currently don't have it set up, but we're gonna be passing forward a token.
So right now, we'll just say sending forward a token and we'll just put it kind of put in some dummy
data for now.
Besides this dictionary, this Jason, that we're passing back, we're also going to say that the status
of this is equal to two a one two a one means that something was created successfully.
Now, if this user name has already been selected by someone inside of this, except here for the integrity
here, this is where we'd want to return back a Jason response.
But in this case, we would say, you know, something like that user name has already been taken.
Please choose a new one.
So I'm going to copy or delete what we've got here.
So we're gonna return this Jason response.
But we're going to use this little, you know, text that we already had.
Again, I love using the existing project here.
It makes our life so easy.
Gay soldiers return air that user names are already been taking.
Please choose a new username.
And we are going to.
Well, let's make sure.
OK, so we've got the air to the.
That all looks good.
And then the status on this is going to be a four hundred.
So let's go ahead and save this and now test out what we've got to see if we can get this to work.
Now, we don't have any of the quote unquote, actual token stuff working, but let's see if it works.
So we're gonna have to create a pretty complex Kearl situation here.
So I recommend whenever you're making one of these, it's always best to just make a file where you
can sort of list out what you're doing.
So I'm gonna go ahead and do a new file here, command and to do that.
And I'm going to save this.
I'm going to save it onto my desktop here.
So I'm just gonna call this Kearl fun.
So I'm going to start building out a curl and we can use a lot of what we had before, so I'm going
to do the up key here to copy what we've got.
I'm going to copy this.
I mean, really all that we're gonna be using from this, we're not going to use the Deshu, but I like
to just get the local host, all that stuff.
So we're going to do API slash.
Sign up.
OK.
Now with this, what we're looking to do here is basically say, OK, we want to sign up.
We've got to make sure that is a post.
So let's go ahead.
And up at the front here to this dash capital X.
And then in strings, specify that we want to do a post K.
That's the U.
RL that we're looking for there.
The next thing that we want to do is specify that we're gonna be sending Jason's we're going to do a
dash capital H.
Then inside of a string, we're gonna say capital content.
Dash type.
And it's really important that you get the exact spelling and capitalization, all of this, because
this is a standardized way that you specify that you're going to be sending Jason Veha and HTP requests.
So we have content type.
And then space app.
Location Flash Jason.
OK, we've got that all there.
And then at this point we just need to pass forward whatever data's going to be inside of here.
So for us, we have to pass forward a username and a password.
So we're gonna do a dash D for the data or the body that we're passing forward here.
And so inside of a string, we're gonna do our curly brackets.
And inside of these I'm going to say user name.
And let's do a call in here and I'll do Nick CAAMA and then let's also put in a password.
OK?
And we'll do our classic ABC DS one, two, three, four.
So you'll notice this curl that we've created here.
This should be trying to sign up for a user that already exist.
So we should get back the responses air that usernames already been taken.
So let's just go ahead and copy this whole line here.
Come to our Terminal K and let's paste this in.
Hit enter.
And oh, he hit a big, nasty air.
So a pro tip here.
I've messed up somewhere, but if you try and read the air here from your terminal like this, you're
really going to have a bad time.
What I recommend is going to the tab of your terminal or window of your terminal that has the air.
And look at it here, because this is much more readable.
In fact, just at the bottom here, name integrity.
Air is not defined.
This is some code that I forgot to import into the project.
So let's go back to, in fact, actually the views of the do app and let's copy the code that gets that
integrity.
Or is the jingo dot dbe.
Let's copy this.
Come back to the other views.
Paste this in.
Save this.
OK.
That we'll come back to the tab where we're running our Kerl.
Let's hit enter on this and a can.
Another error.
This is not good.
Kate named capital user is not defined.
OK.
So we've got to import that as well.
So let's go back to our views.
This is the problem when you copy and paste a ton of code.
You forget all these import.
So let's go ahead and copy this.
Come back into our project piece that there.
Let's try this yet again one more time.
And look at that.
Finally, after a couple of fixes error, that user name has already been taken.
But let's go ahead and modify this just a little bit here.
So I'm going to change this to be Nick one.
OK.
So I'm going to copy this comeback, paste this into the terminal and.
Still another here.
Name Log-in is not defined.
OK.
You are killing me here with the different code that we have.
So in fact, this is not a line that we need.
We don't need to log in the user so we can delete this.
So let's go ahead and save this.
Come back to our terminal here now.
I imagine that user was actually already created.
So if I do the up arrow and try and create Nick one again.
Yes.
It says that's already been taken.
But let's go ahead and change this to now be Nick two.
And look at that.
It passes us back, our fake tokens.
So it's quote unquote, working.
I know that was a little bit of work there, but hopefully that gives you some better idea how to debug
things when you run into issues.
But this is a big step for us.
Now users can sign up for accounts.
This is a really big deal.
We could, in fact, stop right here and just say, hey, this is how you sign up and we'll use that
basic authentication where we, you know, pass in the dash you to have someone put in the username
and password.
But I really think token authentication is a much better way to do things.
And we'll be talking about that in the next lecture.