In this lesson we are going to be the first and point of our advanced API this point will provide us
some really useful information about the user that is currently connected to the application and we
are going to use that information later on while developing the front end of the application to improve
the user experience.
Before doing that however it is crucial to talk about a very important aspect of application.
I'm talking about this single page that is going to be rendered after the user as logged in.
Basically I'm talking about the template what we're going to mount are a View application in the settings
that by filing.
We've set our briefing so that after logging the user is automatically redirected to the home page of
our Web site.
Therefore we now need to create a real path that points to the home page to these addresses and to this
path we're going to link a view that is going to render the template the way we're going to mount our
application.
Let's create a folder called core and inside of this folder we can create it views dot by file and considering
that we want to tell Django that we're going to use this core folder as a package inside of the folder
we can create a done deal.
You need a file and this is enough to tell Django and by phone that you are using this folder as a package
and yet we can now create a review.
Clearly we need to import some classes.
So first of all from Django the country but not author.
That makes sense.
Import logging required mixed Cena.
And from Django dot views dot generic dot BS import template view.
We can create our class so class index template view which is going to extend into quite MC scene and
template view and in case you're wondering why we're using the logo required.
Well that's pretty easy to understand.
As a matter of fact we're going to set out a breast API so that only authenticated users will be able
to interact with the application and therefore it simply doesn't make sense to show them an empty page.
Plus this way they're going to be redirected automatically to the logging forum to define which template
name we want to use.
We can just override the get template names matter which is going to accept the self as always.
And we can define the template.
Name equals index dot HMO and we can just return template name.
There are of course easier ways to define which template we want to render inside a template view class.
However the writing to get template names mapped out is going to be really useful later on.
For now let's create these index dot e-mail file in the templates folder and inside of these templates
we can create all the boilerplate code we need.
This can be.
Question Time and yet inside we can maybe defining H1.
And yeah we can just write the yes and the down below we can define a deal.
We've tidied up so as you see this is very similar.
Basically the same that we've done while developing a few applications in that view.
Yes section.
We now need of course to create the path for our index template view and we need to import it inside
the US note by file so here or maybe even appear from cardboard views import index template view and
we also need to import the red parser function and dysfunction allows us to create parts for our views
based on irregular expressions.
Basically the same data we used to do with the older versions of Django and that's because we want to
redirect every request to these index template view.
This is going to be crucial really really important while developing the front end of the application
and in order to not mess with all the other parts of course.
And at the point that we have defined we can just create the path here at the end of the URL but at
least so let's create a catch all the regular expression and then yeah we can call index template view
dot s view and as a name we can set entry point.
Let's not test this new path let's run Randi development server and let's move to Chrome and then you
can see are we read view.
J Yes because this is the page we've added the user we've I.D. up and we seed of course because we authenticated.
So let's go to I mean let's log out we are redirected to the logging form of course let's try to call
you on page and then you can see of course we are ready to act it to the logging forum.
Let's not focus on creating the end point we were talking about at the beginning of the lesson.
So for now we can close all of these files like so and inside of the users folder I'm going to create
another folder API and instead of API I'm going to create see that ISIS dot by then views goodbye and
yes goodbye.
Let's start by creating the realize that class is we've already said this end point we provide us information
about the user that is currently connected to the application and specifically it's going to provide
us each user name therefore we can create this utilize that to serialize this field from rest framework
import that ISIS and front users dot model import custom use that.
Now we can create plus a user display.
See the riser.
And this is of course models and Atlas is going to extend.
She realizes that model see eyes.
Let's now define class matter.
We model equals custom user and fields equals user name.
Let's not create a view to use.
We have these satellites.
And of course Django framework provides us several classes that we can use to build such a view.
And I believe that in this case the EPA view class itself is a great choice.
So from blessed framework not a response in fourth response and from rest framework dot vs import API
view and then from users that API dot utilizes important user this place utilize it.
And now here plus the current user API viewer which is going to extend the API Europe.
And here we can define the get method which accepts self and request to let's define that either as
user display that either entities finally want to pass the request but use it.
We can now return a response and there's a response we want to return serialize that not data.
Let's create the endpoint itself.
So from Django don't you or else import parser and from users not API dot views.
Import user display API.
View now.
Yeah you get out Panthers equals the list in the bathroom.
Users or even just user which is going to call user display.
He gave you dot s view and as a name for the end point we said current user.
Now considering that decent point will provide us the user name of the user that is currently connected
to an application we need to be sure to only set of authenticated requests.
And considering that as we've said several times you want all the authenticated users to interact with
that application we can't just set the permission glasses and globally.
So yeah in the rest framework dictionary I can call it these key like so but D is of course going to
be default admission classes and I can just define a framework that shows that is authenticated because
I remember you that by default we have allow any let's now move to the understood by file of the project
because of course we need to include d you are registered by file.
So yeah I'm just going to copy this park basted like so.
So this is going to be API and it's going to include users not API not us.
That's all random development server and let's test the new endpoint we get this added data because
the proper name is current use it API view so 1 and so yeah.
So let's restart the development side of it once again.
Let's now move to Cairo where we can go to slash EPA slash use that we get the message authentication
credentials were not provided very good.
So let's look in under
and of course we get our user name.
As expected.