Blog.

Backend Python Part 2

Cover Image for Backend Python Part 2
Bryan Guner
Bryan Guner

So once you open up your PG admin, this is what you are going to see.

So now, what you want to do is to open up this server like this, and then under Postgres SQL, open up database.

Now, if you want to create a new database, just right click on this database and eat, create, and then click on database.

So now this is going to bring a form for you to create a new database.

So we can add a database, my project.

And let's save it, that's gonna create a new database for us.

So that should save all this are saved are created our database, I'm going to show you how to connect that database into your Django project very easy enough.

So this should create.

Give it a couple of seconds.

Sure, let's see.

This shouldn't take this long.

But most of the times sometimes PG admin usually lags or comes through did to your system.

So see, okay, good.

So that has created we can close this one up and open that.

Now that we have these opened, we know that the name is my projects, where we can just do is open schemas.

And then open up tables.

So as you can see the tables is empty.

What we can do now is to come into VS code.

And they want to go into settings just pure to open up our project folder, and then go into settings spy.

So rise, down below are going to go to go We'll see database.

So right here where we see database, what we just want to do doesn't default was a change from engine.

So we can see is Django DB dot backends dot SQL lite three want to change that to PostgreSQL.

So plan this, and then the name should be the name of the database.

So we know that the name of the database is my project.

So right here, I just want to have my project.

And then the next thing that should be clear is the user And the user is Postgres, so just use us us Postgres.

And then after that is the password.

So the password which you set when you first created when you first open up the PG admin is what should be in here.

So I'm going to leave that blank for now.

And then the boost for now is the localhost.

So we have all these.

And then once you have all these in here, the next thing you need to do is to open up your command prompt.

And then right in your command prompt, you need to install two libraries, and the sidecar to specifically be installed.

Cycle two.

And then after installing cycle two, I already have that installed, so I'm just going to bring the out of it.

So I'm breaking out of it.

And then I'll say FP install pillow.

So these two libraries are gonna allow you to be able to connect Postgres to your Django project.

Without these libraries, you're going to receive an error.

So this pillow takes care of everything.

Let's say you have a database that deals with images or files, this pillow takes care of it asac have to connect them together.

So because this requirement already satisfied, I already have that installed.

So once you have this setup, and you have this installed, we'll just need to do is just Python menage does poi make migrations.

So before I say make migrations, let me go into my code and input my password.

So they're right here, there's gonna see make migrations.

And this see the migrations to make.

So let's give it a couple of seconds.

Oh, that shouldn't take time.

So was this big migrations is just gonna suppose just cannot change detected.

So by turn, manage.

Those few are migrate soon, is performing all migrations for this time, taking into Postgres.

So now we will come back to Postgres on the on this table is now right click on it refresh, you're gonna see that we now have a bunch of tables, even with that model, which we created my app underscore fixture.

So if I just drag this down, and I say view, data, all rules.

So I'm going to view all the data I have in that mom fixtures model under my app.

So let's wait for that to come off.

And let's see.

So this shouldn't take a while.

She just take a couple of seconds.

And it shows all the data base, which we've saved from our admin panel earlier on.

issue was right here, because we migrated everything here.

It should be the database it should start using from now on.

So you can do the same for MySQL or Roku, as I mentioned, they're also similar processes.

And I'm very sure you might find it cool and more helpful to use some external database provided and just using the default SQL Lite.

So right now, this is it.

And the data output we do not have anything right here.

But now if we use like if we create a new one now it should show right here.

So this is how to connect Postgres to your Django project.

Now, I hope you understood everything indeed in this video.

And yeah, so now I hope you know how to connect the Postgres database to your Django project.

So guys, that's gonna be all for this video.

I hope you enjoyed this.

Oh, course.

If indeed, please don't forget to smash the like button and subscribe.

Hey, what's up, guys, welcome to this video.

In this video, we're gonna be building a simple blog using Django.

So this video is gonna be in two parts.

The first batch, we're gonna do everything we need to configure the blog.

And then we're gonna start with the posting of a blog, and then showing all the blog posts.

And in the next part, we're gonna have some more features, like getting only some particular words of the posts, right here, and then allowing the user to click on the post and read your posts.

So I hope you're gonna enjoy building this with me.

And then let me show you a quick demo of what we're gonna build.

So right here, I have this post, I can click on this post to read everything right there.

And then it's gonna tell me the day and the time and the title and the posts are very busy blog can do the same for any other one.

And then let's quickly read the last one.

So now all these are being getting from the database.

Now if I come here and add a new post, let's just add some rubbish there.

And it's safe.

Now that is saved coming, I need to refresh.

So that is automatically updated here.

Now, this is what we're going to be building in this video.

And then this template we're using is a very basic template.

As you can see, I'm also going to have the link to this template and the whole source code of everything we're going to be building in the description below.

So you can have that if you want to check it or anything to do with it.

I'm going to leave it in the description below.

And having that said, I think we should just dive straight into this video.

So guys, the first thing I want to do is to create a new Django project.

So right here in this folder, I have this index dot HTML file.

Now this is the file to the template we're going to be using for this blog.

So I already have this template done to, we're just going to be talking about mostly the back end functionalities.

So I'm just going to import this template into my projects.

But first of all, I need to create my project.

So in this folder, I'm going to create my project.

So now I just copied this directory and come into the terminal Mr.

VS code, right up inside that directory.

And then I can just Okay, so let's open up the normal terminal, to go into the photo sources CD.

And then now we can see Django, I think admin start project.

And then we can say blog.

So now this is gonna create a new project in this folder, named blog.

So in a second, it should do that.

So that is done.

If I come here, now I can see plug.

And then in there also, I'm also going to create, I'm going to go in the CD blog, I'm going to say python manage.py.

Start Up Alemany posts.

So I'm going to start a new app and image posts.

So done.

Now if I come into the blog, I'm gonna see a new photo.

So named posts.

So I'm done with this Tommy now I don't need anything yet again.

So I'm going to close that up.

And then now what I just want to do is to open up let me close this terminal.

I'm just CLS and then I can make this short.

And close it up.

And now I just want to opened up project I just created in my VS code.

So I'm going to open folder and then so it should be in.

Okay, right here.

blog.

Yeah.

So blog and post.

So I'm gonna select folder.

Just gonna open that up.

Yeah, and good.

So now, there's gonna quit this.

And then I'm going to come into the folder.

And I'm gonna drag this index at HTML, I want to drag it into the folder.

So now I have everything in you.

I don't need any other thing from the folder.

So right here, I'm gonna start all the things I need.

So first of all, let's run this our projects are made sure that everything is working fine.

So right in here, I'm going to save advice on one niche that's boi Ron's server.

Now this you run our project on the localhost 8000.

Okay, now let me come into my browser.

And then go on as you see the default Django template.

As you can see, it says install work successfully.

So let's head back over closer.

And now let's just start with the URL mapping.

So I'm gonna come into posts, and then I'm going to create a new file, name it urls.pi.

Right in here, I'm going to set up all my URLs.

Now, if you don't know what a Django URL is, is basically the different path you have in your website.

Let's take example facebook.com.

So facebook.com is the main website.

Then when you go to facebook.com, slash notifications, there's another URL, go to facebook.com, slash messages, there's another URL.

So think about that in this project.

So URLs is different spots in our website.

So now, let me show you how to configure a URL.

So I'll say from Django dot URLs, import path.

So this is just a default Django function that allows us to, you know, configure URLs.

So I'm going to see URL patterns.

Again, there'll be a list, and I'm going to save path.

And when I leave him blank like this, it means the home URL.

Now if I put something like notifications, what this mean is slash notification or website slash notification.

When I leave him blank, it means the own URL.

So now I can say views dot index, and I can give it a name of index.

So this is saying that once a user comes to the home, it should go to the views dot index function.

So that means in this views.py file, it should look for an index function and then do whatever was being asked to do.

As we can see, we don't have any index function a view, we're gonna have to create that.

First of all, we don't have views imported.

So we have to say, from views from this import views.

So now we have to use a button, we can say view dot index, and then we just give it a name of index.

Now that's it.

And then right here, let's create a new function and name it index.

That's going to take your requests.

Now we want this to render that simple HTML page.

But before we do that, if we just type return, render this out to render an HTML page, and we do requests index dot HTML, if we do this, we aren't going to see this is either going to give us an error or is going to show us the Django default template.

Now for us to for Django, to know where to get this interpretation, we need to do it in a specific way.

So in the root directory, we're gonna have to create a new folder and name it templates.

And then we push index dot HTML into template, so want to move it.

Now it is inside templates, this is how it's supposed to be.

But we're not done, we need to come to blog and go to settings.py.

So right here in settings, or to pure Why are gonna scroll down to dares are gonna see this deer slash templates.

Now, what this is saying is that whenever a user requires an index, an HTML page, it should go to the base directory, which is the root directory, then it should go into the folder named templates, and look for that exact HTML page.

So when we're trying to render this HTML page index, HTML, is going to request for this index dot HTML in the root directory.

And then it's going to look for a template name, a folder name templates, and then gets done index dot HTML.

So now we have this.

But again, let me scroll up to installed apps.

And I have to add this app I created which is posts, you have to add it there.

So now I can save this.

And now this can be rendered.

So I can save this also and save this.

But if I come here and eat, refresh, boom, nothing still happens.

And this is because we configured all this in our app, we need to configure it for our main project.

So project we know where to look for a URL.

Now, for us to do this, I'm gonna say from Django dot URLs import path, you also have to import include And then we can see the path when you use the boom is gonna include was dot URL.

Now what this is saying is that once a user comes to the home, which is just the main website, it should include the own URL in posts that you are opposed to Yara is our app dot URLs, which is this.

So it's going to look for something similar to this, and this is it.

So we can close this up.

Now we come here to refresh, boom, you can see now that we have this HTML page, we have a written set of whoever the front end set up.

Now, I guess it's time for us to do the main stuffs.

So now, what we need to do is to create a database that is going to store all our blog posts.

So it's going to store the title of the blog post, when the blog post was created.

And then the main body the main text of the blog post.

So we're going to come into modules, which are gonna create a new Django model, let's say class.

And then let's see posts.

So I will say models dot model.

And now we can see, the try to shouldn't be equals two models, dot character feud.

And then we have to set a max length.

Let's set the title should be like 100 shouldn't be more than 100.

And then we have to do the same thing for the body, which is the main posts.

The body of the post was also models towards character field.

And then the max length.

Because says, like, let's say, a million, because some blog posts might be who.

And now, the next thing we need to set is the dates this post was created.

So we can name it created, underscore art.

Unknown as models, dot date, time field.

And then for us to pass in a parameter we need to import date, time is a default fight on libraries, you can see from date, time, import date, time, basically, date time.

So now we can just pass this date time in years.

So whenever this is created, this is going to be saved with it, we're going to get the current date and save it with the source in defaults.

defaults should be equals to the date time.no.

On the nose leave blank to be true.

So now this is good, we can save this.

Now we have our modules done.

And you know, in Django, one ever you create a new module or you add any changes to your Moodle, you need to make migrations and migrate to the database.

So let me create a new seminar right here.

And then all I'm just gonna do is make migrations and migrate, which is python managed.py.

Make migrations.

Okay.

Make migrations.

Yeah, it's updated data on then python manage.py.

migrate.

So this should migrate everything to the database.

Good.

So now, as you know, Django has on default admin panel, which allows you to access all your database and everything about your site.

To access that we can see our local slash admin.

But you see, it asks us for a login username and password, but we don't have debt.

So what we need to do is to just come into our terminal and say Python manage.py, create super user.

So this is going to create a super user.

So we can have our credentials to log in here.

So we can see i'd mean, no, this is just the username.

So say admin, let's leave the email blank and then give it a password.

Then you So now we have this greatest sucessfully, we can use credentials to log in.

Yeah.

So now we have this, we have our database.

This is the users we have on our site, we didn't add authentication or anything complex, we're keeping this very simple.

The only user we are with us, which is the admin.

So let me just go back to the home, where we created this database of posts, then why isn't it here? Now, we migrated this model to our database, but we didn't save it or showcase it in our admin partner.

To do these are going to have to come to admin.py.

First of all, import or say, from dot models, import posts.

So we have this model imported.

And then we're gonna say admin, dot site, dot register.

On the watch, I was gonna register that posts.

When I come here, I need to refresh.

Boom, now we see posts, as easy as that.

So now we have this database, which is very good.

Let's just go ahead and create a new post.

And then let's say, tied to pleasure, say, out to deploy Django project to Iraq.

So for the body, I want to have this report.

So I'm just gonna go to my website collects the blog posts from the otter deploy Django project.

And then there's gonna copy all of these, basically, I'm just copied to this place again.

And then just going to paste it right there.

And then the date is automatically going to get the latest date the correct time.

So I'll save this.

Now we have the first one.

Okay, we don't need this.

We do not need all these.

Yes.

These posts.

So let's just save this.

So we got that.

Let's just create another one.

What do I have here.

So best way to get started with Django sculpted is based on then.

Oh, my gosh.

Good.

Copy.

Paste unsafe.

So we have to let's add one more.

So we can use the database and showcase it right here.

So go back, get one more, let me say installation of bytes.

Okay, that piece paste.

And then, yeah, right here.

And then we also paste this and then good.

Now we're good to go.

So now we have three posts, Raftery blog post on this our blog.

And now we just want to assess everything right here, instead of just having no dummy data.

And what we want to do is I want to use a CLI.

So in this site to add in is obviously going to be the title of the post, like how to deploy a Django project.

And this is going to be the date.

And this is going to be just like maybe the first line of the main blog posts.

So once a user clicks on these, they're gonna take the user to read the whole post.

Let's go ahead and do that.

So now we got this.

We don't need this again.

No, we don't need this again.

Yeah, so now what we just need to do, right here, we can open up our index of HTML, we're gonna need that.

So we're gonna make this dynamic or dispose dynamic recognizes static posts there is there which is just this, we're gonna get all these from our database soon we're writing these.

So to dad's face after import that our Moodle say from dot models import post.

So now we have posts imported we can assess all the data or all the objects in that post.

So we can just say posts should be equals to boasts objects dot all and then we can just pass it right here on sipos.

And then save posts.

Now they should be a variable suppose.

So in this particular line is we have a variable and we are giving it posts, which is this we imported dot objects dot all that means we're getting all the objects from this database right here you can see this object one object to object three.

So I get in all the objects saving it here, and then we're passing it into our template, which is index dot HTML.

So we can see this year.

And now when we come here, we can simply remove this, we don't need this.

So now we're gonna use a for loop from year, so we're gonna use a for loop to assess for those data.

So we're gonna see for posts in posts, so for post in posts, this posts is this variable we passed to our index.

So now we can assess it as a variable.

So as in for post in posts.

Instead of writing the title ad, now we want to write posts, dot title.

In case you're wondering where we get this title from this title is the title of the page of the posts does what we assigned it in the model.

Suppose a title and they should be post created ads.

And then some texts.

So this shouldn't be boss dot body.

Right? Yeah, well, now instead of just on body want to make it like maybe only the first 10 words, they select the first 15.

So we should be able to do something.

Let's leave it for now, then we're gonna change it when we see what our output is.

So now we can end this for loop.

Now, when you're using a for loop in Django, templating, you have to enter a for loop, even though it's just going to go through out the wall, code beneath the for loop.

So it's quite different from normal for loop in Python.

You know, in Python, we use indentations to know when to end a for loop, we use the end for to end a for loop.

So now let's save this icon here and hit refresh.

Good.

So we have Tommy's blood, we about to deploy Django projects, we have the date and the exact time we created this.

And then as you can see, we have all the posts.

So this is working pretty fine now.

But what want to change is that once again, two things.

First of all, we don't want all these posts right here, because if a user can read all the posts from the main page, then he doesn't need to click on that post.

So we don't want all the posts when he wants me to like the first 20 words or something like that, or the first 50 characters.

And then, as you can see, this is 11:30pm.

This is level 32, level 32.

Now 1130 is earlier than 1132.

Now we want the latest posts to be on top.

So as you can see, this is the oldest post.

So if this is the way our blog is Dominus, the first post we write is the one that's always going to be on top, and we don't want that.

So now let's go back in year, and then, to make the latest post be on top are gonna say for post in posts, reverse.

This is very easy.

They'll say reverse, I'm just gonna reverse it for you.

So we got this reversed, D.

So now as you can see, the latest one is what is on top installation, Django best way and now to deploy Django.

So the latest one we add is one that's gonna come on top.

So we have that.

So guys, what we're gonna do next now is to make sure that it doesn't show all the characters of the posts or the old body of the post.

So I'm gonna die in the next video.

Welcome back to the second video in this Django blog tutorial.

This is a second and last video.

So in the last video, we stopped at getting all the list of blog posts, and then making them order in, according to the latest posts.

So at first, when we put it, it was showing the oldest posts at the beginning.

But now the latest poll shows at the beginning.

And I said in this video, we're going to continue why.

As you can see, all the blog posts is showing on the front page, which we don't want only ones like 10 words, or 20 words to show that a user should be able to click, and it will take them to another page to read the whole text.

So that's what we're gonna do in this video.

So first of all, let's remove all these we don't like the way all the bullets is showing, so let's change it to lock on it 20 or 30 words.

So right here in VS code, we just need to add impose the body will say, truncate, truncate words.

And then we'll put a colon and say 20.

So this is going to truncate it to just 20 words.

Now when I hit refresh, you can see now I only have 20 words, I need to automatically put this three dots, which shows that you can continue reading when you click on it or something.

So as you can see, only the first 20 words are showing, which is looking good.

So what I just need to do now is to make sure that user should be able to click as you can see, now just plain text is I can't click.

So let's make exactly and then we're going to take a user to another page.

So right here, it shouldn't be in this old class.

So let's add.

So right here at age 80 going to know where For now, let's base that back.

So this can't calm you're on this gun calm here.

So let's save these.

So this should be going nowhere, but I just checked.

Okay, this is showing, but we don't like our old DS, as you can see show in line with purple.

Now, we don't want that.

So we can just easily add a simple styling.

So they just we don't need to say style, we can just add it in the queue this time.

I said text decoration should be none.

So this is just going to make sure that all the styling doesn't show.

Yeah.

So as you can see the lines, the underlines, doesn't show what we see of this in purple.

So very easy, I'm just change it to color, black.

So good.

But when I click on it, it still goes through that same age, whatever.

So now, once exactly gone, it should take it to a dynamic and unique page.

So everything we're going to be doing is going to be dynamic, you're going to understand that in a second.

So let's come up now.

So what page is the user going to go to once they click on a post, let's go create that in the URLs.

There's great one, unlimited posts, save us those posts, and then we'll give it a name of post must add a comma right here because the lists, save it.

Right now we don't have any function new views named posts.

So let's go create that.

So say, def posts, requests.

And then let me just render a simple HTML page.

Post dot html.

So now we save this, but we don't have any page like this.

Let's create that quickly.

Right here.

New File, because the HTML Okay, so we have with an S.

So let's come here and update that post return s.

So that's good.

And now what we just wanted to do is to make this kind of dynamic.

So we want to right now we can go to slash posts.

But nothing is showing just a blank HTML page, which is this page.

So we want to get all the data so we once like once a user click on this is gonna take us to that slash posts, but it's gonna be like slash posts slash one slash post slash two.

Friends page with different URLs.

So these blog posts since is the first post is gonna have an ID of one.

So it can be slash post slash one, this can be slash post slash two, this can be slash post slash three, if you understand that, and then we're gonna get all this dynamically from the database.

So let's do that quickly.

Right here, we can just say, we can give it an integer or a string, just like a string to avoid any errors.

So a string, which is p k, so this is st slash, post slash a string, which is p k.

So we can see that coming to views.

So right now we are collecting pk.

And then what we just need to do is to say that posts is equals to those dots objects, not written as the same just object dot get the one which ID is equals to Pinky.

So once we have these, we just simply pass it to the HTML, let's copy that here.

And paste.

So what we're doing is, once a user gets this URL, so let's say is going to be post slash one.

I know that slash one is the ID of, let's say, one of these of these.

So obviously, this we have an ID of one because it's the first or maybe an ID of zero, maybe.

But once we get the ID is gonna be passed in this URL from the URL is going to be passed.

Yeah, you can see that this function is getting that PK, which is the name we gave it right here.

And then from that became using you to filter our database.

So I've added a new variable.

And then once it's been assigned to the variable is posts, which is our Moodle, dot object dot get.

So it's going to get to that particular posts, which has the ID of pk.

So then once we have that, we're just passing it to opposite HTML.

Because we did index dot HTML, we can go there now and access that post.

So right here, let's just copy the index dot HTML code and use it.

Let's paste.

But still, to me is blog.

Birds go to slash posts.

Okay, so we need to add something that is slash one.

Okay, so it doesn't have an ID of one, zero.

So let's come into our data base.

Cookie, no, I D, for now.

So we need to create a particular ID.

As you can see, for now, it sees very various ad type posts as no attribute objects, so it doesn't see any posts with this particular object, which is kind of weird.

But so what we just need to do is to make sure that we are doing the correct thing.

So we're basically going to come back here.

Now URLs.

So we're passing the string as be gay ID.

And then right here, we're collecting data and then filtering it, which is good.

So post data objects, okay.

So objects with an S, suppose dot objects dot get.

So now I come here, give it one.

Okay, so post object is not iterable.

Yeah, for sure.

So what's the saying in post dot html, every column here, as it gets here, was in a for loop to get this now, but in our index, because we said post dot objects dot all, which means is getting it as a list or as a dictionary, or just a list of something so we can loop through it doesn't mean you have iterable when it says something to do his music and look through it.

But when we samples that object dot get only getting one specific value.

So we can look towards specific values, just one thing.

So that's why it gives us this arrow dots post object is not iterable.

So in our posts, you must remove days and remote produce.

So now if we come here and click enter, again, we don't have anything here because wising posts instead of post return is, but let's leave it at that.

But we have this now.

And then right here on to the post.

So our simples are try to pose a created on posts.

That's bad.

But now we're not truncating.

Any words, we want every single thing.

Now, let's see.

So as you can see, it says how to deploy a Django project to Roku.

And then it gives us all these.

So this is what we need.

So right here, let's give this on each one.

Also, because it's in title.

And then putting the rest is good.

So let's save it refresh.

So how to deploy a Django project to Iraq.

And then we have the date that was created, and then we have to post for these.

So first of all, let's remove this particular a tag, we don't need that again.

So that is gone.

So right here, you can see that this is perfectly working.

But what we want to do now is from the homepage, we want the user to click on this, I need to go to that particular posts.

So we have to come to index dot HTML.

And we'll say, instead of going to an ash tag, says oozes lash boosts with an S, let's be sure.

No one just boosts this slash boss.id.

Just like this, now, the issue of everything working.

Now let's click on this, you see this installation of Python, the date it was created with with the old posts, and let me go back again, or click on another one, you see this mentioned that particular post.

And if I click on another one, it takes me to that particular post.

So this is how to beauty of simple, very, very simple blog using Django.

Now you got some more advanced features, like very advanced features like images, you can add comments, like he's actually able to comment, you can add to different types of text, so the user can design his text like fonts and stuff like that, you can add various other things.

It's just the basic, the basic fixture in our blog is the crud functionality, which is create, read, update, and delete.

So a user should be able to create a new post, which you can do from our admin panel, and then should be able to read the post.

So as you can see, anybody can call me I read this, you should be able to update the posts.

So if I come here, installation of Python, and I say installation of Django, and I say safe.

And I come here I need to refresh.

As you can see, it changes to Django, it should change to Django, because that's what we designed it for.

So I really hope you guys enjoyed this video.

And is actually also be able to delete a post to Konya and delete a post is going to delete higher.

So we think the leads Yes, I'm sure with no me on each refresh, it's gone.

That's gonna be all for this video.

Once again, I really hope you guys enjoyed building this with me.

Hey, guys, welcome to this video.

In this video, we're gonna be using Django to build a weather detector up, so is just gonna get the current weather situation in a particular city, which is a search for.

So right here, if I just search for London, and I click enter, it says London, it gives me the country code, the coordinates, the temperature, the pressure and the humidity.

So this is what we're going to be building in this video.

So the template which has this HTML file, and the source code, which is the final score to define our project, everything is going to be linked in the description below.

So you can check the description to get the source code for the front end and the back end and everything you need.

So having all this said, Let's dive straight into the tutorial.

So guys, the first thing we need to do is to first of all create a new Django project.

So we're going to create a new Django project now and we're gonna name it weather detector.

So to do dads we have to say in Django admin, start project and then we're gonna say weather subject to something like this.

Then we just hit Enter.

And now this is gonna create a new folder named weather detector in the directory under folder is going to contain the Django default boilerplate.

So we can say, CD does go into that folder.

Now we are in this folder.

If I press dir, I'm going to see now that I have managed spy, and whether the data.

So after doing this, we can just immediately create the Django app we're going to be using for this project.

So I can say python manage.py.

Start up.

And then we just want to name this weather like this, then you'll see a new folder is going to be added named weather to doodies.

Noisy, we have weather.

Now we're going to import this project into Visual Studio code.

So I'm just going to open up VS code.

And then open folder.

So I'm going to open up that folder in VS code.

So I have it in weather up weather detector.

So this should just go ahead and open my project right here.

So let me just close this up.

So what we are just going to do now is to just glide with configuring our templates.

So as I said, in the introduction to this video, all the source code to everything we're going to be doing on the template file, I'm gonna link it up in the description below.

So you can get to the template file, which is the HTML file, and the source code to your finished project.

So if you are stuck anywhere, you can easily just check the source code.

So we're just going to create a new folder in the root directory, and then we're going to name it templates.

So now let me pop up my terminal, or let me just use my command prompt.

Python managers view I run Siva.

So what I'm doing now is that I'm running this project we just created on our localhost, so Django is going to run it on localhost Port 8000.

Just gonna copy these, come up into my browser and paste it.

Now you can see it's showing us that yeah, this installation works successfully, what we don't want to see is what we want to see is our own HTML file, our own template.

So let's go back to what we're doing right here.

In templates, we're just gonna get our HTML template, which we want to use, and then we're going to fix it into this template folder.

So this template folder is just basically going to contain all the HTML files, which we need all our template files.

So I've got my template file right here, which is indeed the HTML.

I'll say it once again, that this index HTML file is going to be linked in the description below.

So I'm just going to drag it pastes in templates.

So now we have that.

But you know, that's not enough to tell Django that it should render the page.

So we need to come into weather detector, which is our main project.

And then in settings.pi file, we're gonna scroll down to where we see templates.

Now see templates here, that we're gonna look for.

Here's where we see dir is right here to save base dir slash template.

So what's templates? So what this is doing is that it's telling Django where to look for our HTML files.

So anytime we request a want to render an HTML file, is going to come into base directory slash templates, which is basically in this folder, where we have our HTML file.

So after doing this, let's just add the app we created, which is weather.

Let's add it to our installed apps.

Nice.

So now we can save this.

But again, this is not enough for Django to render that HTML page.

So now we need to go to URL routing.

So right here, you see that once we come to the home page, we have this default Django template.

So to Django, where to look for a template, but have not told Django though, when a user comes to our home page, what HTML template should you render.

Now to do that, we need to use URL mapping or URL routing.

So we're going to specify that once user come to this URL, which is our home page.

It should render that index dot HTML folder, which is right here.

So right now, I'm just gonna come into weather.

So we need to create a new file named urls.pi.

Right here These URLs are p wise where our URLs is going to be stored, say from Django dot URLs, imports, but to our importing path, which is going to allow us to specify different URLs.

And you'll see from the views from your import views.

So also important views, I'm going to explain why we need this in a second.

Now what we need to do is to have a new list named URL patterns.

Now it should be equals to path.

And then we'll say views dot index.

And they will say name should be equals to index.

Now, what we're doing right here is we're importing path from Django URLs.

So is a default Django function, which allows us to specify a different path right here, you can see where we use it.

So this from dirtybird views is basically this file.

So we're importing every function which we have in this file, every class anything we have in this file, why putting it into these URLs with pure.

So now, when we specify a new this year, the URL pattern is a list variable, which is going to take a list of all your URLs we're going to have on this our site.

So the first URL, which I specified is the homepage, that's why it's blank.

If I want to specify, let's say, slash signup, it should be something like, let's say register, this is how I'm gonna do it.

I can just leave it blank it just register or is login, I can do exactly the same thing.

Something like this.

Boss, this is the homepage, I can just leave you blank.

Now Django knows we're talking about this page, the home page.

Now when I say views dot index, or I mean is from this views.py file, which we imported, go to the index function.

Now the index function is where we're going to tell Django what HTML templates or whatever we want to do, which we want to render.

Now, this name is just basically the name given to this particular URL.

So whenever we want to request for the URL, or go to that URL, we use this name.

Now we can just save this.

And now we know that we don't have any function named index in views.pi, we need to do that anyway, you know, create a new function, I'm kinda silly, name it index.

So this index wants you to take a request.

So we're just going to say return, render, and then request.

And then index dot HTML.

So this is requesting for index dot HTML in Templates folder.

Nice.

Now we can save this.

Well, we could call right here on each refresh, it still doesn't work.

Now, the reason why this doesn't work is because we configured all these URLs only in our app, we also still need to configure it in our main project for Django to know what we're doing.

So we come to weather detector, which is the main project folder.

in there, we go to urls.pi.

And then we scroll down, where we import path, we also want to import include, after importing include in the URL buttons, which is here already, we didn't write this is there already, we can just say path.

And then we'll say include with the two arrows.

Now what this is doing is that we're creating another home URL, we're now in the URLs.

py file of the main project.

And then once it comes to this room, it should include whether or not URLs that means whether is this app, which we created right here.

And then we go to the URL spy file of it.

And then anything we do to the same path is what should be done.

Yeah, it's just including the URLs.

So if I come here, you see we have the same blog path.

So anything we do here, I was wondering is taking views dot index, our are we doing a view dot index, rendering this index of HTML.

So now when we come back here, and just hit refresh, now you see that we have this HTML page.

So for now, you can see all these are just dummy data, country code, country code coordinates coordinates.

So what we want to do now is that we want the user to be able to search for something like London.

When is it enter, we want to show the current weather details or weather status in that particular city? So I just gonna come back to VS code.

So right, yeah, the index.

Now I went to the column into templates, and in index.

So right here, we're going to scroll down.

So what we want to do now, if we scroll up, you're gonna see that in this index, we have a form already.

Now this form is using a POST method.

The first thing we need to do in this form is basically in this input, this search bar, which we have here.

So this form is a post form, as you can see, using the POST method, in Django, anytime we're using the POST method, we always want to make sure to add CSRF token, something like this.

On the squat, okay.

Now, once I save these, and I call massage London, it gives us this error.

Now, because I've not refresh the page you see of the error, but take a look at the error, we see it says forbidden.

If I go back, I need to refresh.

Now I've loaded the new one.

And I searched London again.

You see now that I don't have any arrow, nothing is working yet.

But we've bypassed that arrow, which is good.

So what I want to do now is basically just st this detail, this input right here is this input, which we see input type text name, city.

And then once you click on submit it, you send this detail to this view, back to this index dot HTML view, is still what I'm sending you back to this page.

But this time around, we want to be able to get some details on things.

So this is easy for us to do, what we just need to do is just specify the data which was sending back.

So right now, right here, what we're just going to simply do is come here, when make sure that you set an action.

And the action should be blank.

Now the reason why these action is blank is because when we eat so search is sending all the data, which is only one actually sending this London data back to this home page.

Well, this time around want to have something yes of country code coordinate, want to have the real country code, which should be London, UK or something like that.

And then later, we're also going to remove these.

So that's when he is after his country decides, by me, when his office comes to decide this shouldn't be the only search, we're gonna do all this also.

So after adding the option, and then we're able to send now what we just need to do is to come into our views.py file, and then collect that.

So first of all, collect the data which was sent, we'll say if requests dot method is equals to posts.

They want to say, city shouldn't be equals to request dot posts.

City.

Now what are we doing right here, when I come to this page, from the normal URL bar, I'm using a get method to access this page.

When I when I send a form using a POST method, I'm not sending a form to this page, the method is different, as you can see now is a POST method.

That's why we're going to say if request or metal is posts.

Now the only time we're gonna use post is when we're sending a form.

So we're gonna get if the metal is post, that means obviously, the program's you know, the form is coming.

Then once we know a form is coming, we just need to collect the form, say request dot post CT.

Now, this city is this index, this input right here you can see there's a name of city, if I change this to place, then also needs to come here and changes to place.

But for now, let's go with city to make this big again.

Just go with city, or then city top, you understood what we did, right? So we're just gonna save these.

And then let me just first of all, after sending the city to this place, let me just pass it back to the HTML just to make sure we know what we're doing.

So we can do something like this city.

So we're just going to save and then if I come here So it says local variable city referred before assignment.

Now this unbundle Kira always happens, anytime you are using an if statement, and there is a variable been assigned without adding an else statement.

Now we just need to do else.

Sit CT should be close to blank.

Once I save this and come back and eat refresh, we do have the arrow again.

So anytime we come across the arrow in Django is always very, very, very common.

You just can just simply use that to take you over that.

So right here, we have country code.

Just see h1, and then there's a city.

So right now, if I refresh this page, and I hit London, now you see excuse me back in London.

So that's just first verified.

Okay, yeah, we're already passing a data value from our page back to our page, as I explained earlier, using different methods in a POST method is tomorrow.

But now, obviously, we don't just want to see London, I want to see is the country code, which is not really relevant, but I think is still key, we can get that the coordinates, the temperature, pressure, and humidity, so weather status, and details of the current city, which you search for.

So for these, we're gonna be using an API, which is called Open weather map.

So it's a website, which has an API for you to use.

So you can get those details from their website.

But before you can use their API, you have to sign up to their sites and then get an A, it's totally free.

To sign up, you get you can get an API, and I'm going to show you how to use their API.

So first of all, you need to come to your site, which is open weather maps.org.

And then once you log in, you can just go to API keys right here, and they are going to see your own key.

Now you can this is free to adjust in for testing purposes, this is totally free, you don't need to buy a subscription or anything like that.

Nope.

So we're going to be using this API key to do what we want to do.

So I'm going to show you how to use the API gets the details of any city, we search research London, it was such a cry, it was such a cape town with such Delhi, Mumbai, anyway, who can get the current windows situation right there, right here from our Python program, or Django site.

So that's gonna be all for this video.

And then we're gonna finish up these projects in the next video.

So guys, is the second part in this weather websites tutorial.

So in the last part, I stopped at telling you guys to get the API key from open weather maps.

So once you have your API key, you can continue to follow along.

So this is the second and the last part of this tutorial is just a two parts tutorial video.

Now, this API key is very important for you to be able to access the API.

After getting this API key, you can continue.

Now this is where we stopped ads, which was where we just pass London back to this page.

But now, whatever city is our search, want to get the details, what we're gonna do is go back into VS code.

Now that we're able to send in data from this form to our views to our templates, we can easily just get that data and then get the weather situation in that city.

First of all, let's remove these towers for testing purposes now.

And this also.

So now that we have done all this, what we just want to do, we have the city.

So right here.

So now that we have city want to assess this open meta Maps API from this code, so we need some libraries, we need to import JSON.

Now JSON is where as the JSON is, when we send a request to the API is going to give us a response in a JSON format.

So we need to import JSON to be able to actually get and then you know, pass or filter this data.

And then we need to import URL lib those requests.

So now that we have this done, we are very good to go.

So right here, I'm just gonna say something like risks.

Now this race is the request was sending to open weather maps.

Now what I'm just going to do is to say, URL, leave those requests that URL open.

So I'm going to open a particular URL, and the URL is going to be HTTP API dot open, open with a mob.org.

Slash data, slash 2.5.

Slash with question mark, then I want to pass in some parameters, say Q.

Now Q is going to be close to the city.

Now this city is Q is actually a totally means query.

So it's going to be close to the city that the city will want to get the details of.

And we use concatenation at the city.

After adding the city, what we just need to add right now is our API key.

So we're just gonna say an ID should be equals to now those only inputs, the API key which we collected from you.

Paste it right there.

So now we have inputted, we have input our API key.

So I'm just gonna say dot read.

Which two parenthesis.

So now that we have this done, we're just gonna go on a new line.

And then what I just want to do is to say lists.

Let me just say JSON data.

Now this is the data I get after sending a request to this URL.

So I'm going to say JSON data should be JSON dot loot with an S in order to load risk.

Nice.

So now I have the current weather details of this city stored in this JSON data variable.

But now I want to make this JSON data, I want to change it to like a dictionary, a Python dictionary.

So it's easier to access, I can just simply say data equals two.

So what I just want to do is to say country shouldn't be streamed.

And then from JSON data, see s Y is and then we'll say country.

So now after having these, we just use a comma to get some other details which we need.

Now let's see what we need right here.

That was amazing.

Why puts this down.

So we know what's funny, we need the coordinates, the coordinates.

So another one is the coordinates.

Now, they actually have their documentation where you can get out to use all these, but I already know how to use it.

So you can just follow up with this tutorial.

So you can also get some more things about that specific location, even more than with our situation, the guys who are getting the coordinates are getting the country code.

Now these are more about geolocation.

So you can use the API for some more themes can just go check the documentation.

But for now, let's stick with this.

So coordinates, and then also a strange making sure is a strange.

And then from JSON data, what we just want to do is C o r d, and then another one after it, I want to get the longitude, which is short enough to learn.

And then to concatenate if it doesn't look like it space or something.

Now want to concatenate it with the latitude also send JSON.

And then what we don't need to do is also say, C or D.

And then right here, we're just gonna say lat, which is the short form of latitude.

Now comm put a comma to get to the next one.

So we have the country code, we have the coordinates.

Now let's get the temperature which is basically what we need.

Let's change it to temp strange.

And then JSON data, then what we just need to do now is to just the main main exam, which is the temperature plus gay.

So this key is basically in Kelvin, so it's not in degrees Celsius degree far away, it is in Kelvin.

So you can also you know, use your Python code to convert it very easily.

But for now, let's just stick with Kelvin.

And then let's see, we need to pressure and humidity nice.

So just a pressure.

We forget our pressure string.

JSON data mean other than preasure.

Nice.

So now you can just add our comma.

Then the last thing we need is humidity.

Humidity, then we say the same thing string.

Then we just say JSON data.

There we just say mean and then we just say humidity.

Basically.

You meet the tea.

Nice.

So calsonic Homer just to avoid any heroes, go right here.

And then we can just come down.

Okay.

So we're, I think we're done with this data since we have it closed here already.

So now we already have everything we need inside this data socios data shouldn't be empty.

So now what we just want to do is after rendering this, we just want to pass data.

Now we can access this from our HTML, which is quite cool.

So now let's come in to our HTML.

So right now, instead of all these country code, coordinates, we can just use these same thing for for the temperature also, for the prayer, humidity.

And I think that's what we need.

So now let's save this.

Let's just save these.

And we come back here.

Now this is the key now you see that there is nothing in the outputs.

So now there's eight slon.

Done.

Now press Enter.

So it says budget quiz HTTP, we come down here.

Unable to receive, so we are having some errors.

Let's go and check was Yeah, from this line, the error is coming from this line.

So let's go see.

Views dot p y.

So let's see, most of the time, this happens because of HTTPS.

Now, let's go back to our page, London, we eat enter.

So we see the error, basically.

So let's copy this URL in which we're trying to assess.

And then let's open it in a new page, just to make sure that Yeah, everything is working.

Okay, now, the reason why we're getting this is because right here, there is no equals to.

So let's go back and change that.

So you can see that whenever we are passing a value can see there's supposed to be q, which is a query equals to CT, then app ID equals this.

So let's just say query equals two then CT.

So I think that should fix our problem.

Let's save again.

And we come back here and search for London.

Now you can see very, very good.

We have country code.

Great Britain, which is where I live, I'm just saying coordinates.

We have the longitude and the latitude, then temperature is sodre.

points.

34k.

Yeah, I think that's correct.

Then pressure, which is DS unlimited D so ever these which we need.

And I think basically, that's all we need for this project to be working.

We can do some little tweaks like going back here.

Right here in the index.

We just want to have right here is like an h1 and then let's pass in.

Gum, yeah.

Yeah, this CT.

So let's make sure the CT it goes to blank.

Then, let's see CT is CT.

No safeties, we come back here.

In year we can put CT.

Let's save this and check.

As you can see, we have our page coming in as HTML.

So I'm very sure the reason is because there was a syntax error right here.

So go the dictionary, we just have to cut that.

Do the candles do something like this.

So let's save it and go check it out again.

So we eat enter with four codes to run.

Let's open that up.

So we have an invalid syntax actually.

So the reason is because let me come back here is because we are not Supposed to pass a dictionary on, you know this like this.

So we just need to do is this.

So said data should be equals to data.

Now let's save these.

And then this should work back.

Let's wait for it like two seconds or so.

Okay, he's reloading.

Yeah.

So now let's see, unless it's London.

So now we have London.

But you can see that we have all these not showing.

Now the reason why we do have it shown because it doesn't see it.

Now, as a dictionary, again, it sees it as a normal variable.

So we're gonna have to come here, index, and we'll see data.

Data that data.

So you need to add them.

It Oh, and therefore humidity also.

Now when we save these, come here, but yeah, each refresh and submit again, you can see now we have London.

And we also have all these details.

So that's basically how to just fix that.

So first start.

So let's search for another city, actually, let's see legace.

Now Lagos country good Nigeria, which is Angie, now we have all these well, cool temperature, then, let's see another setting and then we move on.

So that takes us now to exhaust contract with us.

And it gives us the details.

Now this is what we want in this video.

One more thing that we want to pass is when a user comes to this site, we don't want this to show one displays to be blank.

First of all, there was a search history show.

So we can also do that by first using an if statement.

So after rule, right here, by going to say e if data dot country code.

Then if we have these they should be out the country code should be we have these initial these else, which is our else statement alleges entity if we don't need else.

Now let's save this and see each refresh boom is gone.

So what we're doing is we're saying if data does contract code, that means if we have anything like this, then you show all of these words, even though it's obviously this will not be there.

So now let's go and check it again.

London, we'd enter now we have this border, we just come to this site.

Normally we do have these are this is very good.

This is what we want.

So I hope you guys enjoyed this tutorial, which was really fun for me beauty.

And also, if it did, please don't forget to smash that like button and subscribe.

In this video, we're gonna be building a simple chat application using Django.

So this is going to be built with Django and a little bit of Ajax, so a little bit of JavaScript in it.

So we're not going to be using doing anything so complex, we're not going to be using them WebSockets channels or anything like that, just Django on Ajax.

And the link to the source code of everything we're going to build in this video is going to be in the description below.

So the template file and everything we're going to build, you can access it.

And this video is going to be in two parts.

So this is the first part and you're going to have a second part to conclude it.

And yeah, so let's go ahead and test this out.

So this is the room name or user can create a new room or they just say let's give it like a room of code Dean.

I don't know I'm just giving it any room and then less and US team to now enter the room because he is blank for now.

Let's enter that same right here.

We are let's enter as john.

Now we enter.

So now I can see I guys almost like it sent.

You see it shows 42 this is a real time and I got a What's it send you see it shows 42 so this is the simple application we are going to be building in this video.

Now all these data you're seeing here with the date and time with the message and the name are going to be saved in our database.

So as you can see, we're getting back all these messages in real time.

It's actually a fun project to build, and you're going to improve your skills in Django.

And mixing is really a bit of JavaScript to even notice it.

Let's dive straight into the video.

So guys, the first thing we need to do to get started with this project is to obviously install Django.

So as I said, in the intro, we're gonna be using Django to do this chat app.

So let's make sure we have Django installed.

So I'm just gonna say Pip, install Django.

Now this command line is going to install the latest version of Django on my computer.

But I have Django installed already.

So it should tell me requirement already satisfied.

And note that if you're new mark, you should type PIP three, install Django.

There's just a small difference between Windows and Mac, what, there is nothing more than that.

So as you can see, it says requirement already satisfied.

So let's just wait for that to opt out.

Oh, yeah, I cancelled it.

So let me close this up.

And now we can just create our project.

So I'm in the directory where I want to create my project.

So I'm just gonna say Django tyffyn admin, start project.

Let's name it Django chat.

This is it's Jango chat.

And in this video, we're not going to be using channels and readies and the popular way of using a chat app, what we're just going to be using is a little bit of JavaScript, which is Ajax.

So we're going to be using Django and implementing Ajax to make our chat functions real time.

So you're gonna see what we're gonna do in this video.

So right now, we can just go into that and say, Django charts.

So we are in this.

And then if we press dir, we can see right here that there's money.py file, and we have this folder.

So let's just what I like to do is to create my Django app immediately.

So now I can do Python manage dot view, our start up our name, just name the app chart.

So now, this should create a new app named charts.

And we should have another folder right here, named chart.

So right here, you can see we have a new folder.

So we don't need this command prompt again, let's come into VS code.

And then let's just open that up.

So I'm gonna open a folder.

So the projects we just created, I'm gonna bring it into VS code.

So project Shut up.

I have it in here.

And it is yeah, this is it.

So I'm going to say Select Folder, so it should pop that up.

And give me some seconds.

Okay, so that's done.

Let me close these anomalies.

So now we have all these right here.

So the next thing that I want to do, the first thing I like to do is to just add my template file.

So the template file, when I say template file, I mean the HTML files in which we're going to be using in this project.

So first of all, let me say templates.

So I need to create a new folder in my root directory named template.

And in here, I'm going to bring in my template files.

So I have this template file designed already in this video, we're not going to be doing that from scratch.

So if you want to get it, as I said in the intro, I'm going to have a link to the source code of this project in the description below.

So you can go there and get the template file.

So right here, if I open this up, this is where I started the template file.

So I'm just gonna drag and drop into VS code.

So we have it right there.

Now the next thing we need to do is to go to our settings, and just configure the template and this add this chart to the installed apps.

So first of all, let's come here.

And then there's our charts.

So that is added to the installed apps schoolzilla up and then we come down yet writing this, I can just say pays the day when I see these slush templates.

So what this command line is doing is that it is telling Django where to look at our template file.

So when we are doing the URL mapping, which means when we looked telling Django where all our URLs are all the URLs of our project, and we want to render an HTML page to show for a specific URL, we need Django to know where to locate that HTML file.

So this line of code says directory so I don't need to go to the base directory.

So the base directory shows the root project right here.

This is the base directory.

And in the base directory, Django is going to enter into a folder named template.

As you can see, this is a template.

So Django cannot assess all this files inside this template folder.

So let's say we instead of naming this template, let's say we name it something like files, Oh, we don't need to do is to come here and change it to files.

But yeah, we are using templates.

So there is no specific name you have to give this is just make sure that this and this correlates.

So yes, we have that done.

And then we can save.

Now I don't think we have any to do with settings file, again, now we just need to do is to set up our URLs.

So we've come here, and then we need to create a new file called urls.pi.

To now setup URL, we're just going to say from Django dot URLs, impulse pass.

So this is gonna allow us to be able to configure the paths.

So now let me just say from the input view, so we're gonna use this and I'm going to explain why we're important this second line in a minute.

So now let's say you are buttons equals two lists, the first one, which is empty the root directory, we used or let's say home, and then let's give it a name.

So now we have this.

So we're important these views, because we're saying that when a user wants to go to the home directory, it should go to the views dot home.

So it's going to go to views.py file, which is this file right here.

And again, to look for a function name.

So whatever we're doing it functions, we're going to be happening to this URL.

So I just did a second like to import views.

So we can says any function we have enough, which is right here.

So right here, we have to have a function named home.

I'll let you just take a simple requests.

And then there's return say return render.

And then requests for now want to request for home dot html.

So that's it, I'm gonna call me on it.

So the last thing we need to do for this particular URL is comes in Django chat, which is the main project folder, and inside the URLs have to follow these ledges closes up.

So right here in path after importing path, want to import include, and then I'm gonna say was include charts dot URLs, and we're gonna save.

So now I'm going to pop up a new terminal.

And then I want to run my Django projects just to make sure that this URL configuration works successfully.

So let's give that a minute to set up.

So it's very easy to run a Django project to actually try to follow up with this video.

I expect you to know at least some basics of Django so you know what's going on around here.

But when in time I'm doing my tutorials are like explaining everything I'm doing even if you're not, you don't know anything about Django so you know what's going on.

But having no basic knowledge of Django before watching this will be helpful.

So to run our Django project, we're just gonna say Python managed.py run server.

So that's how easy it is to run a Django project.

So I'm just going to wait for that to run on the local host which is localhost Port 8000.

So okay, right.

Yeah, it gives us an heiresses shrink add new objects.

Oh, so we're gonna come here so it's safe on Nana it also cuz I guess it hasn't updated.

We think gives us that same error.

Okay.

Let's see.

views.

That's boom.

Here.

It is not supposed to be full stop supposed to be a coma.

So let's save that again on should be running.

Yeah, yeah, so dad's running on this.

So if I just Ctrl Click is gonna open these particular URL on my default browser.

So okay, they just come here and open it up ourselves.

So you're just going to type in the localhost, which is 12700 on port 8000.

So that was open and ready.

And then we should see our HTML file.

Good.

So as you can see, now we have Django charts.

And then we have the basic form, which we're going to use in this project.

So this is the basic HTML file we're using.

So a user just gonna add to the room name, and then his user name, and we'll be able to enter a room.

So let's head back to VS code and continue with the coding.

So now we can just close this up.

So let me just open a new terminal down because we're gonna need it later.

So now that we have these, what I want to do is to set up the model that we're going to be using in this project.

So I'm going to set up two models, one is going to be for the chatroom, and the second one is going to be for the messages.

So I'm going to come into models.py file.

So right you, all I just need to do is to create a class, I'm going to name it room and gather models dot model.

And the only thing I want this to average is the room name.

That's all Mojo's towards Jakarta field, and unleaded max lens of 1000.

So that's what the room name.

So I would have given it another attribute of ID.

But in Django, each model objects as an ID generated one if a new object is created.

So like these days, another attribute named ID Oh, don't need to write that ourselves.

So this is good to go.

And the second one want to create is the message.

So this is the module or the database that is going to be storing all the messages.

So say models dot model.

Right now, I just gonna say value.

Yeah, I think value is a good name.

So value is like the message the user wants to send like a Oh, you're doing what's up.

So that value want to give it so models dot character field, also, Max land, so a message can have up to like a million characters.

And then let's give it a date.

So which date wasn't this.

So before we can use date, we need to import something named date time.

So from date, time, import date, time.

So we're gonna say models, dot date, time field, not only date, view, date, time field, and then what want to pass in year, this is not going to have a maxlend.

What this is going to have is a default.

So defaults will be equals to date, time, dot now.

And then let's just set blank to be true.

Just copy this, paste.

So the next thing we want to have is the user.

So which user is sending this particular message that we want to use a ledger give it a million user name shouldn't be able to double it's just to avoid any errors in this project.

And then the last term I want to add is the room.

So want to know which room is this projects been is this message been sent to.

So this room is going to be like, we can use another Django model, like a relationship model, like the many to many of you the foreign key to link this with this room.

But we're not gonna be doing that in this video.

So this is also another way to do this.

So one was submitting a new message is going to have the ID of the room, which is going to add a link to this.

So you're gonna understand all these in a moment.

So let's just save this.

And what we just need to do is to migrate this to our database, so say python manage.py.

Make migrations.

And then after making the migrations, whether it's gonna have to migrate, so that's done by dawn manage.py.

migrate.

So, yes, we have the migration done.

Good.

So it has been migrated to our database.

So now we can go into our Django project and go to slash admin.

But before we can answer the admin panel, we need to create a super user.

So Python money creates super user.

So let's just avatars admin, leave this blank.

Give it a password.

Let's bypass good So now we have a supervisor created.

Let's close this up.

So we need to register these models in our admin file.

So first of all, we have to import the modules from those models, imports grew an important message, admin, dot site, dot register, register, boom.

And then there's also register message.

Now we'll save this.

Now that come you go to slash admin.

Now he's gonna ask me to log in, let's give it a second to load.

Nice, he says I should log in, let's gonna log in with the details that I just created.

And then, as you can see, now it does take me to this admin panel, and I have the messages and the rooms.

model, the Django model right here installed.

So coming to messages.

For now, obviously, we shouldn't have any object here.

So what if I want to add a new message, you're gonna see that it has all the fields we need.

So let's just go back to the own.

And then let's open a new tab, go back to the home page and leave this like this.

So now we have all this.

Next, next thing that I think we need to do now is to make sure that once a user put in a particular room name, let's say the room name is Django, just Django room and the user is like, Tom.

So what is the entire room, it should take a user to another unique URL for that particular Django for that particular chatroom.

So what we need to do is to first of all, check if this room exists in our database.

So if, let's say Django, for example, is the name of the room, I want to enter the room.

If it's a new room, that means if someone has not created it before, we first of all want to create this room, and then enter, but there is a room that already exists, we just want to enter the room and see all the messages.

So let's just take this off, and let's go through that.

So first of all, we need to let me remove this, we don't need this again.

So right in our URLs, we need to have a new path.

So this path is gonna be the path for the room, it's gonna be the URLs for the room.

So I'm just gonna say so it's gonna be a dynamic one.

So we'll say strange, dudes name of Rome.

And then we'll say slash.

So this is the Rwandan slash anything want to add to it.

So say views.

dots room, I'm gonna give it a name overall.

So this is good.

And then let's come into views and create a new one.

I'll name it room associate room request also, under just copy this, it should also render the room dot html file.

So this is where the room dot html file is.

So it's rendering the room dot html file.

Very simple.

And now, what we just want to do, so this is not just taking a request.

First of all, as you can see, right here, we're passing a variable into this particular URL name room.

So we need to come into views and also collect.

So now we have all this collected.

But if we come into home dot html, so right here, scroll down, go down, down.

Right here, we have this form.

So want to send this form to an action that was a user click on Submit.

It's your first sent those data to a view that we check if this particular room as I said or not.

So let's let's create that view.

So we need to create another URL, actually.

So let's name this check view.

So this is a view that is going to check for what's going on.

So Oh my God, why is that? So let's just copy these views.

The check room is the name of check row, as Michelle design, comma, and they're right here in our views.

Let's check room.

Just think request.

For now, let's pass.

Let's save these knowing komiya and save these.

So now we have this URL.

So if I go to slash anything is automatically going to see that as a room.

But what I want to do now is to come into my own dot html.

So right here in my home that HTML, I want to add some things to my form.

So this is going to be easy just to add where this data is going and stuff like that.

So first of all, let's say the method it's using to send this data is a POST method.

And then the action, where is it going, it's going to check for you.

So now we have the room name, the user name.

So in Django, we are using a POST method, we must make sure that we have the CSRF token CSRF underscore token.

So you must make sure that we have this obviously, now we have the room name, and then the user name.

So this is good to go.

So now we are sending these with the room name and the user name to this check for you.

Now, right here in the check view, before we do anything, we want to check if that room name exists in our database.

So before we can do that, first of all need to import that particular model so safe from, we can see those models, or we can see from chat those models anywhere you want to do it is fine.

From standard models import, we want to import room, I want to import message.

So for now, we don't need message berlage as important down because we're later going to use it.

So right here in the jag view, what we just want to do is to say room, because the requests dot posts.

And then bescom yummy shower.

So does a name of room name.

So we're getting this name, room name.

And then the second one is the username, obviously.

So let's make sure user name correct.

So a person DS, and then what we just want to Jake was gonna say if room dot objects dot future, so our future needs file t name equals room.

Now want to check even this exists, then let's do something.

So what we're saying right here is that if a room got object of future damage, it's gonna check even if there is an object in this room model, which has the name of room, so the name of this room that the user sent.

And then if that exists, wants to do something, what we just want to do is to redirect the user to this to the room, which is a trim function.

Before guys redirect, we need to import it.

So import redirects.

Good.

So we just say, return redirect, and we're redirecting the user to slash room slash room dot name.

And we don't need to say that name, because you already have that name.

And we know it exists.

So we're redirecting the user to the room.

And then what want to pass also want to pass the user name of the person that wants to enter this room.

So we know we send in a message with entering and doing stuff like that.

So now, we can just say slash user name.

So right here, first of all, we should have like, these user name should be equals to unreligious.

Add the user name.

So that's why we took tissues and so returning the user now, to slash room slash user name, then equals to user name.

So this is good to do working on then even doesn't exist.

So if this is a new room that the user is just creating, but want to do is rather new.

To have a new variable, say new underscore room should now be equals to room does object dot create.

So want to create a new one with the name of room.

So we're gonna get that room and then create a new room with that particular name.

So now we just say new room built safe.

So now once we have this saved, we can redirect the user to that page.

Exactly the same thing we did here.

So I hope this you understood what we did in this check view is pretty basic, pretty simple stuff.

Religious safety safeties, and good test it out.

So right here, does it refresh.

Let's go to our rooms, he will see that we have no room to let our new room unless you say something like, let's give it programming.

They just say programming.

coders.

Okay, good.

And then they say, Tom.

So now let's create a room.

Now you can see everything is working perfectly.

It takes us to this particular URL named coders.

So this is our app slash go does then user name equals to.

So we are the name of the room, and then the user name, so the user that entered the show, and then when we come here and eat refresh, we can see that our room has been created successfully room coders.

So that is good.

And then don't mind this message or see now that's just the basic template that we brought in earlier.

So this is what we're going to change this to be real time message, and oh, or deciduous, or demo.

And they are going to change this to the room name, obviously.

So I think we have these, and then let's check.

So let's go back, and then try to enter this cooler room again.

So now let's make sure that he doesn't create another room with that same name of CUDA.

But he just takes us into that room.

So let's enter with another user, this time I could change.

So he brings us to that same room, but with a different user this time around.

So once we hit refresh, we see that we don't have a new object created, it just takes us into this existing one.

So that's what we did in our check view.

So that is pretty good right now.

And then, since we have these, or we can just do is to come into room dot html.

So right here, this is where we need to do some work.

Next, I can just close that up.

So instead of having name, okay, let's see, right here.

Okay, good.

So now we have all this grated, the next thing we just want to do, first of all, we're going to remove this.

And then once a user input a message, and it's sent, we want the message to be stored in the database with the details of this room and the user.

So you're going to see what we're gonna do.

So right here, now, what we just need to do is to first of all, come into room dot html.

So right here in this form, we need to create some things which are going to allow us to submit that obviously, so in the form, we're going to listen, the form, right should be down below, discloses.

disclose DSL.

So this is the form here.

So now in this form, let's just have some things like CSRF token, as I always have CSRF underscore token to this, just to make sure that whenever we use a POST method, everything works.

So you can see now that I put a CSRF token by I didn't do something like method posts.

So that's because we're going to be using Ajax to submit this form.

So if I come here, normally, and then I type in a message and it's sent, that page is gonna reload.

And that's, that's what we don't want in our chat.

So we want you to be real time, want everything to happen.

Without this page, even thinking of refreshing.

So once we hit something is gonna send I was it sent without page refreshing is gonna be created in our message database.

And then later on, we're also going to make it show you automatically in real time, without any page refreshing.

So this is where Ajax comes in.

So Ajax is a very cool feature.

I have a ton of tutorial on Ajax on my channel.

So it allows you to do things like get database in real time, asynchronously, some cool stuffs that you can play around with Django.

So this Ajax is not with Python is actually with JavaScript.

So we're gonna be using some basic JavaScript.

Now, to use Ajax.

We just need to make sure is that You have this script loaded in your template file.

So this particular line, let me just scroll across so you guys can see the lines.

That's it.

So once you have that imported, you are good to go.

So first of all, right here, you can see that we have this form.

So we want to click Send.

So you right here, you see we have 1234 inputs.

But yeah, we only have two inputs shown.

So if you see the type of this is eating, and the type of this is also eating, so we don't want the user to write anything new, we're gonna automatically give it a value ourselves.

So the user name is the current user that logged in.

And then the room ID is the ID of this room the user is in.

So before we can continue, we need to come into our views, get these two details, after getting details, we pass it back here, and then implement it in our form.

So let's do that quickly.

So now right in our room, we just need to say user name, first of all, so how are we going to get this username, you can see no bad, bad, bad, bad, bad, bad.

So user name should be because now let's just go back and listen to a storm.

Okay, good.

So make sure our URLs is working.

So you can see now our URLs, we have the name of the room, and we have the name of the user.

So since we have it in this URL of this view, we can just get back this details.

So let's say user name should be equals to.

So I'm going to request for its request that gets that gets, Shall we do it.

And then we'll say username.

So it's stored in a variable of username, as you can see.

So now, in this scenario, we're going to get the value of Tom, if this is theme are going to get a team with this area we're going to get.

And the next thing we want to get is the room details.

So you can see that we have this room.

So this code does is what is being passed here in this room, we have that read.

So I was gonna say since we have the name of that room, we're gonna use this name to assess the database.

So we're gonna see room on discord details.

And then we'll say it goes to room objects dot get name equals from.

So what this is doing is that we have a new variable, obviously.

And then from this room model right here, dot object dot get is getting the particular model which has the name of this room.

So we have the name of coders in this case.

Now that we have all those details that we need, all I just want to do now is to pass it into our HTML.

So right here, let's make sure we get that.

First of all, we put a comma.

And we have these.

And then let's pass the user name.

Copy this past a user name.

And then one of us the room details also.

So first of all, what a buzzer room.

Actually, if we have the room details, we need to pass the room.

But let's just do that.

Let's copy and paste.

So now we have all this sent to our HTML.

Now in our HTML, we should be able to access all this easily.

So let's save this file.

And right here in rooms.

First of all, let me scroll all the way up.

So instead of room name, now that I have the name of the room, I can just do room.

So this room is actually DCE.

Okay, this shouldn't be days.

Good.

Save that again.

So we have this room.

So let's first see that change and make sure everything is working.

So instead of one name, we should have queued us.

So right here now you can see we have queued us.

Good.

Now we're going to scroll down back to the form right here.

So the value of the user name is the current user, we should just have these.

And then the room ID should have real details that Id like this.

So I get in this room details that was sent here.

So as you can see, remember that the different details of the particular room from the model.

So what we're just doing here is getting that particular model and getting this ID remind the beginning I said once you have a new object, the object automatically On ad, so I get in that ID.

So now that we have all of these issued, why we should be good to go with using our Ajax.

So just use Ajax, I'm actually gonna just open a new script, right here down below the body, as given here.

So to save time, I'm just gonna paste in this code.

So what this code does, you can see just a piece of JavaScript.

So let me explain each line by line.

So this is document.

So what this is doing is that once we load this document form, we're saying that that also meets of this post form.

So what we're saying is immediately a user clicks on the submit button of this post form.

So this is this Submit button of the post form.

So what do we want to do, we want to have a function.

And we give this a value.

So we'll say e dot prevent default.

So what this prevent default does is that normally in a form, once we'd submit the page is going to refresh or go to another page.

So this prevent default is going to prevent that from reloading or going to another page.

So now we know that the page isn't going to reload.

Now we're going to use our Ajax to send that details to our database.

So what we're doing now is that we're saying Ajax, and then we're saying the type is posts normally weren't using Ajax, we know, well, we're gonna use something like method.

But since we're using Ajax, we don't need that.

So already assessing the type post, and the URL is sending it to send, I'm going to explain this URL in a bit.

And then the data is just basically what the data user name with the image tag of username, room ID, and then the message.

So as you can see, everything is user name, mighty message.

That's just what we are sending.

And then the CSRF token, this is how we implement the CSRF token right here.

So the success function, we can just say something like message sent.

So I just commented this out was let me leave that like that.

And then, yeah, let's just leave this like this.

So what this is, what this is doing is that the sources function damage if the message or some debt to database, we're going to create a particular message.

So this is going to be accessed from the views.pi function.

Going to go back to that.

And then this so what this is doing is that once a user it on sent and it has been saved in the database, we want to delete these once we delete that particular line from the input.

So what I mean is that, let's say user type something right here.

And then these are it's sent.

So once that has been saved, we want to clear this particular input to now be blank, just like the way you see normal chat services.

So right here, it gets this, this shouldn't be good.

What we just want to do is to have a new URL, and we're gonna name this URL sent.

So as you can see, it's sending all this data to a URL name sent.

So first of all, let's save this up, and come up with a new one, say sent.

And then we based and then we based so we have a new URL name sent, then wants to create a new function for it.

So just the way we did something like this check view, remember, so this check view first checked, if that room is available, or not available, then before it did whatever it wants to do.

So we're gonna do something similar in the st.

So we're just going to do, we're going to assess the st.

So let's scroll down a new function sent requests.

So these are just basic functions.

And then, right here, you can see we're sending all this data to this st.

URL, so let's just access them.

So we're just gonna say the message should be equals to.

So why isn't a Post's method from the front end associated with quest those posts and then it's same message.

So I like cross checking to avoid any arrows.

So here is an a variable message their room ID, then use a name.

So those are the three we need.

So Nope.

gamier based based, and then this should be the user name.

And then this should be the room underscore ID.

So now copy that.

just pasted the essence of the same thing.

So we have all these done.

So now that this view as all this data, which to us are kind of inputted, or we just want to do is to store that data in this message database, we'll let that quickly load.

Okay, I know why this error is coming up, just because we didn't save some changes yet.

But let's keep going.

So you know, we have this modules, which is message, so want to save all those into this module.

So now, since we imported the message module up here, remember I said we're going to later use it, so we have that imported, we don't need to import it again.

So we can just say, new message.

No new message, we're gonna say message is no just message actually, message does objects.

Not slash Wait a dots, objects dot create.

So I created a new object right? Now the value, remember we used value shouldn't be the message which we collected.

And then the user, remember which user should be the user name, which was collected, and then the room.

So what we want to store is the room ID.

So room should be equals to the room ID.

So now that we have this, all we can just do just say new message that safe, so we just do new message that safe.

So we have that saved visuals, return.

Now, we are not rendering any HTML page, we just want to return back to the front end or to the JavaScript the message.

So we're gonna give it an HTTP response.

But before we can do that, first of all have to import each year.

So we'll say from Django dot HTTP, import, HTTP response.

Good.

So now we can see return an HTTP response.

We can say message sent successfully.

Good.

So now we have this.

So this HTTP response, this is how I just saved the file.

And this is our gonna, so this data, remember I said on success dummies, if everything works perfectly, we're going to function with this value of data, and then want to alert data.

So this data is this HTTP response.

So when we alert, it's just gonna say message sent successfully.

Now, we did pretty good.

So let's go check out what we do.

Now, this should be working.

Good.

And then in messages, you can see we have zero message.

So now let me just type Okay, first of all, I got to refresh this page.

refresh, refresh.

Good.

So now, let me just type a message like, Hey, guys, and I sent you can see it says message sent successfully.

Obviously, later in the tutorial, we're gonna remove this or just using this for confirmation.

And then once I hit send, you see that the message doesn't show year again, which should be not as come on it's refresh.

I think now that we have a new message, so it says a guy's value.

And it comes from the user term, which we see here.

And then the room with the ID of one.

So the room with the ID of one is obviously this restroom.

So if it's another room is going to tell us the room with the ID of two.

Let's cross check.

No, that was awesome.

So I say okay, let's go to the home page.

Now I want to create another room and say, back end developers.

And now told me is going to be here.

Now let's create the room.

Now let me see a developer's an IDE sent the message and successfully the message displays is cleared.

Now we come here to messages, we have two messages now, a developer's we got the date and the time, and it's coming from the user to me and the room ID is true.

So if you've gone to rooms, you see we have a new room greeted with the idea of a back end developer.

So we can use this to assess the messages for each room later.

So this is working pretty fine.

We what we've done to this point is that a user can create a new room, enter that room, so it's gonna be on a dynamic URL, and then the user can send a message and that message will be saved in the database.

So in the next This video, what we just need to do is to make sure that all once the user enters a room, all the messages of that room, first of all show will load like normal charts.

And then once a user submits a message I need to send is automatically going to show right here.

And then not just only for the user for also another user anywhere, the person is also going to test that with an incognito mode.

So I hope you guys enjoyed this tutorial to this point, I really had fun building this.

If you do not forget to smash the like button and subscribe.

Hey, guys, welcome to the second part of this Django tutorial.

So this is the second and final parts in this chat app with Django.

So in the last video, we stopped at saving all the values into database.

So we add two models, which were let me quickly come your messages and room.

So this room is gonna create a new room, whenever a user wants to create a new room, and then this message is gonna store the messages.

So this message is like the database for storing all the message.

So as you can see, we have the value of the message a developers, we have the date and the time, we have the user which submitted our message, and we have the room.

So as saying the room with the ID of two.

So we didn't watch the last video, you should definitely watch that before these.

So what we just need to do now since we have all the data is stored, I think we don't have too much of work to do, we just need to get all the data and showcase it to you.

But we are we need to do a little bit of extra work is to make sure that this data showcase in real time.

So if a user from another mobile phone or from another place in the world, texts have also sent a message, we want to see it right here in real time without even refreshing this page.

So we're using Ajax for this last video, we use Ajax so that when a user submits, so if I come here and say I testing.

And then I see that I click sent, it says message sent successfully, this page didn't refresh, and then it was cleared from you.

Welcome to messages, you're going to see that I have a new message, I tested the date and time user and the room ID.

So now we use Ajax to do that.

Now we want to use Ajax also, again, to load these messages data in real time.

Let's get right with that.

So right here, we just need to add a new URL.

So if we are just using normal Django to just load the messages, you know, we're just gonna come into the views, we have a new function, or we don't even need a new function, which is going to come to the room, and then we'll just specify the message and send it to the HTML file, and then just showcase it.

But if we do that, once the user updates or create a new message is not going to be updated in real time.

So we need to make that real time.

Let's go into URLs, we need to have a new view for getting all the messages.

And this URL is going to be dynamic.

So since y is in a dynamic URL list, copyright is past this.

So this is slush gets messages.

So if you can read these, this is saying that gets messages slash a particular room that you want to get the message.

So we're gonna have the room name.

So if the room name is called us, like right in your digital name of Jesus, but develop back end developers.

So we're going to be like get messages slash back end developers, they will say views that get messages and the room gets messages.

So this is good.

And then it is going to come into our views.py file, create a new function, gets messages should take a request.

And it should also take for now, let's pass.

So now let's save this.

So now this is working fine.

Now, what we just want you to do is to come here, and then want to get all the messages of that particular room the user is in.

So now that we know we have the room name, so we can use this room name to get all the messages of that room.

Now, when we get this message of this room, then we're gonna return a JSON response of all the messages.

Then from our front end, we're gonna use Ajax JavaScript to assess that JSON response and showcase it to our user.

Now, first of all, we need to make sure that we are importing JSON response so that we can use that so right here in HTTP, we can say JSON response.

So right here, now we're no longer gonna pass.

We're just gonna do that to save room details.

So this is a new variable.

So was this in the room model objects that get on then what gets in the room, which has a name of this room right here.

So now that we know we have that room that we're looking for, let's get to the messages associated with our room.

So let's say messages.

Good, should we get calls to message so this message, Moodle does object dot future.

And they want to filter with the room, underscore underscore eight continues, don't worry, I'm going to explain this.

If you don't understand it here.

It's room details.id.

So what this line of code is doing, now, let me go back to what we did in the first tutorial.

So right here, a module created a new model name message.

Now this message, what is is just the model that's going to be storing all the messages on this platform or the messages.

And then we add four attributes, the value of that message data, that message was sent to the user.

And now what we are specific about is the room.

So this is what I want to talk about this room, is which room was this message being sent from? Like, which room does this message belong to? Exactly, that's the word.

So this room is just specifying the room ID of the message that this that this message belongs to.

So now that we know that room ID, we cannot get all the messages.

Now ever come back here into views.

We're saying message those objects, the filter.

So now we want to filter with all the list of the data we have here.

With the room ID that's a contains the room details.id.

Now, I don't think we should even say contains I think we're supposed to say with a room that equals to room details or ID.

Now this is that more futuring without the messages with the one in which the room is this room details.id.

Now, let's just return in JSON response, return JSON response.

And then let's return it's the middle variable of message messages, actually, let's say you should be fine, then, at least obviously, we need to say that at least I will say messages, values like this.

So this is returning a JSON response.

And is returning it as a variable of messages.

So this is what we're going to be using to assess it.

And then we're done does a list of messages, which is this messages of value.

So we're getting all the values from you.

That's very good.

Now we have this done.

Now what we just need to do is to come into our room, dot html.

So right here, this was the AJAX function we used in the beginning for submitting the message.

So for now, let's just comment that out.

And I think we can close this up.

So right here, should be good to have another script.

So right here, we're gonna have another scripts.

Let's just have it right here.

Good.

So let's create a new script.

So in your I'm going to paste a particular JavaScript code.

So what this code is doing, I'm going to explain how this code line by line.

So this is saying that once we load this document, which is this page, then want to want to do everything in this function.

So this is set a set interval, what does that data What does is that anything inside these brackets are the function is going to be done again, and again, and again, with this particular amount of time, which is one seconds.

So this means that all these Ajax function and getting all the requests, everything is going to be done every second.

That's why we can assess the data in real time.

So once an update is made the next second, we have that data updated already.

So now we are now using the AJAX we're saying get So this time I like getting a particular data from the URL gets messages slash room.

So you remember when we did it right Where's that right here gets messy slash.

So now we are giving you the room.

Where is the room? In our views, remember that in the room view right here, we sent in room.

So this room is the name of this particular room.

So I think, yes, maybe this last particular name of the room.

And then if we've got everything successful, and then so what we have right here, when we are getting, we're getting this JSON response, right here.

So this is what we're getting you, then if it was successful, you can do console dot log response, we're going to show the response in girls, which is that particular data.

So I commented that out, because we don't need it, but we just want to do is to show it in our UI.

And then I said, display dot empty.

So what this is, is, so if I come here, you'll see that I have a new div tag with an ID of display.

So first of all, I made sure that this is empty, I removed everything there.

Metallic gonna remove all things I just commented out.

So I removed everything in there.

And then I looped through, so I say for varkey, in response those messages, so for each value in this response, and this response, which includes was JSON, it was saying dot messages, as you can see is this messages of everything we get here.

So as seen for very key in response messages.

Now, what are the new variable over just named it temp? Just put that, and then now you can see that we have this particular is it div tag in here.

So this is the div tag we use in here.

So we just specified a new JavaScript variable with the HTML tag.

So is that the same thing? So it's div class, container Doc, everything.

But now in you can see that we just added dummy messages.

Hello, everyone.

How are you guys doing? But right here does know what's out there.

Again, right here.

What is the IDS response? messages key dot user.

So this first of all, is the user name of the person that sent the message.

And this is the value of the message, which is like a whatsoever, guys, and this is the date.

So now we have this in an HTML tag, while I was gonna append it to the div tag that has this hashtag display ID.

So now this is empty, then we're gonna append whatever we get into this div tag.

So after having that done, I would just add an arrow.

So if anything happened, if we add an arrow, we just say an error occurred.

So this should totally work.

Now let's save this up.

So if I come here, so back end developers? Yeah, let me refresh.

Okay, so as you can see, now I have two messages.

So if I go into my Chrome, there's no Chrome, this is Microsoft Edge Internet Explorer or something like that.

So if I come into my dev tools, and I come into console, now let me is refresh again.

Okay, normally, I'm supposed to see all the data I'm getting in real time right here, where I guess, not Chrome.

But you can see in this HTML is occasionally changing every one second.

So let me show you what is going to happen right now.

So if I send something like WhatsApp people, and I say sent.

So automatically, you see that that we have that message here in the HTML, you see that is North Korea detailed in our code, but that's what that JavaScript code is doing.

So this is supposed to show everything, I guess, because I'm not using Chrome.

I don't know why.

But yeah, we can skip that, obviously.

And then, let's see.

Let's see.

Let's see.

Okay, good.

So these were we're looking for network, not console.

Sorry for that.

So you can see that this is getting some data every one second.

So it's updating.

Updating, actually, this is not even what I'm looking forward to.

Okay, I guess.

So in caso were supposed to have, oh, yeah, I know why the problem is, so right here in our sources function.

We didn't we I remember, I commented it out.

So let's save this again.

Is it refresh? So now you can see, I have this message without reloading.

So three messages for this particular room, a developer, I was a pastor.

So you can see now that we have three Good, let me have a new one like a, and then just hit enter.

Now you can see we have four in real time is updating.

So let me show you what is cool about this.

I'm going to open a new incognito mode.

So in private window, I don't even think I've opened this in his browser before.

So I'm just gonna add that there.

Is these? Yeah.

Okay, good.

So where is that? We have you? Yeah.

Good.

So now, let me just assess.

Well, in back end developers, so let's enter this same room, or space states for now? We are I think we are Yeah.

As to me.

Now.

Let's enter as john.

And ls it enter room? Now you see that it loads all the messages of this room? Now, let me say he told me are you doing? automatically, you see, it shows you and it shows in my own side also.

So it's real time.

So if someone is in another part of the world, it's gonna work.

So let's say we post this online.

And then we have two people connected in different places, once a user sends a message here is going to show right here in my own platform a real time, as long as I'm connected to the internet.

So it's a pretty good feature, actually.

So let's make sure we got no errors.

And in this test is a game.

So let's just go to the home page.

And then on the beach.

So now let me create a new room.

So let me say, let me just say, code, let me just name it code.

And now let me enter as broad, something like this.

Now, interdivisional, you can see now that there are no messages, actually, I can put something like, if there are no messages, I should say, no messages in the room or be the first sentiments of the like that.

We don't need that for now.

So code.

And then let's say that she's another name, Jessie.

And now we need to enter.

So now we have these two blank rooms, to now say, a who's online.

I'm 18.

And you see, it's automatically shows here without any delay.

And I can see I am.

And it shows here.

So this is very, very good.

So I might post this online, maybe when I'm done with recording this tutorial.

And if I do, I'm going to leave a link to the description, the description.

So if you want to chat with me there you can.

And I guess we're pretty much done with this video is something's right here.

Okay.

How's Gooding going? Yeah, good.

Good.

Yes, of testing this.

So yeah, this is very good.

I love what we built in this video.

With you guys love what we build.

And you lie you like watching this video.

And if you watch this video to this particular point, I appreciate you because not everyone watches to the end.

In this tutorial, we're gonna be talking about the Django rest framework.

So the Django rest framework is a library, which allows you to Butte API's in your Django project.

So let's say you have a Django project, or you want to build an API so that others can access your data, or you're just trying it for fun, or just want to build your skills, that Django rest framework is the best way to go.

It allows you to easily build API's with a lot less code.

So this is this, the rest framework is very, very easy to set up.

And I'm going to walk you through everything in this tutorial.

So we're gonna start from the scratch, we're gonna, first of all, install Django, and then we're gonna start a new Django project, then we're gonna install the rest framework, and then I'll show you how to integrate the rest framework into that Django project.

So without wasting any time, let's get straight into it.

So right here, I have my command prompt, and I already have it in this directory where I want to create my Django project.

I want to first make sure that I have Django installed.

So just pip install Django.

This command line is for installing Django using Python package manager code Pip.

Like also install Django in some other ways, like easy install or just with GitHub code directly, but this is the best way to go.

So I already have Django installed.

So it should be telling me requirement already satisfied.

So we should give that a minute, it should bring that up.

Now we already have Django installed right here.

So it's taking longer than usual.

And then I can just get out of that.

Let me just close by I have Django installed before, you should go ahead and install it.

So now let's start the new Django project using the Django admin start project.

And the let's say, the RF, which stands for Django rest framework approach.

So Django rest framework project, to that command line is gonna start a new application or a new Django project in this directory.

So this is a directory right here.

So you can see now that it is empty for now.

And boom, you see that we have a new project that has been started.

So let's come back.

Yes.

So now that we know we have a new project started, let's just CD into that project.

First of all, so we see we have a Django project and CD into it.

So as close that up by press di R, now I can see that I have money, just Pew II and di F.

So this is my standard Django project.

Now the next thing I want to do is to install the Django rest framework.

Before I install Django rest framework, let me bring this Django project into VS code.

So let's open up VS code, close this up.

And then in fall, that's going to come to open folder.

And then it should take me to the folder which should take me to the file manager.

So I can go into that folder I think is to F toots, then Chang, ti F.

So this is the folder.

And then once I select that folder, obviously VS code is going to open that up.

So as VS code is doing its magic, let's go back to the command prompt.

And then what we just want to do now is to install the Django rest framework.

So let's say Pip, install Django rest framework.

So this is how easy it is to install the Django rest framework, just a simple PIP command.

So this command line is going to install the Django rest framework on our computer.

So again, I already have the Django rest framework installed, so it should tell me requirement already satisfied.

But for you, it should go I understood that if you don't have that installed, so let's come back into VS code as that is installing.

And then let's just make everything clean here.

So we can quit this.

So we can see that this is a Django project.

So for this project, we are not going to create a Django app, at least for now.

So later, we're gonna create an app where we're gonna use serializers and stuff like that.

But for now, we're just gonna stick with just the Django project and the URLs of UI file in it.

So that's where we're gonna put our views.pi.

And everything we're going to be using.

So let's come back to the command prompt.

As you can see, it says requirement already satisfied.

This means that I already have the rest framework installed.

So now that I know I have the rest framework installed, I can use the library in other module in our project.

So now, what I just want to do is to create a new file First of all, and then they rename it views.py.

So in this views.py file does where I'm going to code just like a normal Django project.

First of all, let me import render, say from Django dot shortcuts.

So from Django shortcuts is the command line.

Not command nine actually is the module.

So the shortcode is like a class, I think like a function in the Django library, in which you can input some things.

So we import render.

And now, that's just for rendering a Normal template for, but I want to use the API view from the rest framework.

So the rest framework provides us with an API view class or function, or whatever it is.

So with that API view, we're going to be able to access a lot of type of API's that is available in the Django rest framework.

So when we use the API view we can do we can use something like a get request or port request.

And some other things that are being rendered or are being given to us by the rest framework.

So first of all, say from rest underscore framework.

And they want to know the views, then we'll import API view.

So I put in the API view from the rest of the among those views.

And then, as I said, this API view is gonna allow us to create a function or a class on it, so that we can be able to use all the everything available in his API view.

But we'll also is an API view, we want to obviously send a response.

Now, for example, let's say someone, another developer tries to access our API, once it sends a request to our API, we'll want to give the developer a response want to give him some sort of results.

So let's say user send a get request just to get a list of a query set or something, or at least over data, want to give the user back a response or data data? So first, give a response, we're gonna have two inputs, response from the rest framework associate from rest framework, import dot response, first of all, import response.

So what this is doing is just same from rest framework that, Oh, nope, this is wrong.

from rest framework dot response, you just want to import response.

So that's it.

Now that we have these everything imported, we can now create a class to inherit from this API view, so that we can get a lot of methods that we can work with.

So let's just have a new class.

And let's name it something like let's say, test view.

And then API view.

So this was naming this, this view.

And then this, this view is inheriting from the API view, which is right here.

So what we can do know in that order this class, we want to have a get function, which is just gonna be like a get request to this API.

So said, Jeff gets, and then roughly says, self request.

Alright, last arguments, and then keyword access keyword arguments, keyword x, just like this.

So now that we have these functions, what we just want to specify is a data.

So this is a test view.

And then in this test, we have a get request.

So we want to like average data in which we're going to send back to the user or just give us a result.

So same data.

For now, let's just hard code our data ourself.

Later, I'm going to show you how to use the jungle model so that this data will be like from the database you have or something.

But for now, let's just have a simple dictionary that we're going to send back.

So let's just have a dictionary saying something like user name.

Let me just see admin.

And then number of years, I select number of years, lots of the user has been active.

And then we can give it something like 10 years.

So that's actually a good user.

So this is just the data, the user name is admin, and he has been active for 10 years, let's just make demo specific number of years active.

Remote these, we don't need that.

So we'll just use active to last 10 years.

So now we have this data that is in a dictionary format.

So this is a data I want to send back as a response.

So by just going to do is to say return response of data.

So what we just did was that we use this response in which we imported from restaurant the response.

And then we're returning a response of this particular data.

So now we have this done.

What we want to do now is I want to go set a URL for this so that we can actually test this when we run it on our localhost.

So now let's come into URLs spy, right here.

I just wanna Remove all this needed.

And then in year, first of all, let me have a new path, which is going to be empty.

Now this empty path shows the own URL.

So like when you just go to your website, let's say like google.com, boom URL is that empty path.

So that's that.

Now, what we can just do is to first of all, import that test view from the views.

Let's import this video.

So now that we have this view imported, we can easily use it in here.

So this is the UI.

And then we can just say test view.

Since it's a class based view, we have to add.as view.

Now the reason why we're adding this.ru is because the view in which we're using is a class based view.

If you're using a function based view, you don't need to do that you can just say, test view year as the view.

But since y is in a class based view, we must do this even though it's going to give us an error, and Django is not going to recognize that other valid few.

So test view.as view.

And the ledger gives you like a name of something like test.

So now we have all this set up, we have it linking to the own URL using the test view.as view.

And we'll give it a name of test.

Now, I want us to just run our server and test it immediately.

But if we do that, while I'm gonna see nothing, we might even get an error.

And it is why am I getting an error is because right here, what we did was we installed the Django rest framework.

Immediately, we just came to start importing everything in our project.

But you know, if you work with Django for a while, you know, you actually need to do some second configurations, to be able to use some particular type of libraries, not all.

So for this Django rest framework, we need to add some things to our settings.

Now let's come into settings.py file.

First of all, the first thing we need to add is in our installed apps, we need to add where we rest framework, surprisingly, so right here, we need to add rest framework in our installed app.

So Django is going to recognize it.

Now after adding that as well to do with the settings, what we just want to do is under the URLs, we need to add one URL in which the restroom Oh, my goodness.

So first of all, we need to import include.

After that, we can just pass API.

I think for both of them, we're just gonna do like a comma, and give you this a slash.

And then we're just gonna include from the rest framework, dot URLs.

So that is duffel, dat, and we'll give it a comma.

And then we can see.

So now, we can easily come to a command prompt, and just the Python managers Pew II run server.

So now when we run this server, we can come right here.

And then if we go to the Oh, actually what I'm going to use this for now, we just have to set that Oh, because the Django rest framework requires that.

So when we go to the UI, it should give us this test view.

And this test, you should just be docking get to just like either sending a get request.

And then we're just gonna give it a response or this particular data.

So let's save this also.

And let's come back and see what's going on.

So this should start running in a second.

Because we felt that normally, Django doesn't take time to run by guest causes a new project, and we're running for the first time.

So let's just give me a minute.

Okay, so we have that done.

And then let's come back to a browser.

Let's open a new tab.

In the new tab.

Let's go into our local hosts with the port of 1000.

Because that's Django default URL, something like that.

So give me the time to load.

Okay, I can see the title loading already.

Let's give it a few seconds and Yeah, good.

So this is what we see.

Now, you know, We didn't design or this page or this fancy UI that we're seeing, we didn't design.

That's what comes with the Django rest framework, it actually gives you this nice looking template to showcase your API and basically test all your API's.

Now, as you can see, is giving us in a dictionary right here is giving us user name, admin ESR, tF 10.

And that is exactly the same thing we are writing in data.

So normally, the API uses it gives us a response in JSON format, that was given us a response in dictionary format, because those words, we actually set up right here.

Normally, it shouldn't be a JSON format.

So we're dealing with real data, real models, you're gonna see what I'm talking about.

So as you can see, choosing the GET requests, and then the HTTP two addresses, okay, so because it's allowing and get requests, because we specified the get function, right here.

Let me remove this safe.

Because we specify the get function right here.

If we specify the post function, we're also going to talk about that soon.

It's gonna show right here that we allow in a POST request.

So that is basically the basics about this Django rest framework.

So if I come here, and then I, it's Jason, you're gonna see now like, gives me this in JSON format.

So right here, he just gives me a blank JSON format, no template, nothing, just a blank JSON formatted as if I go to this URL.

But normally, we just get the homepage, this is what it shows us.

So this is good.

And I hope you're starting to have a grasp of where the Django rest framework is above.

Now, to be honest, it gets more complex than this layer, I'm going to walk you through every single thing you need to know to get started, and I'm going to simplify every single thing.

So let's just come back to the rest framework page.

And if we scroll down, right here, in installation, you see the exactly what is doing here is what we have done in our project.

So you can see it says install using Pip, including any optional packages you want.

So this is where we install the Django rest framework.

We can also install markdown on the Django filter, but we don't need that for now.

Because they are optional packages.

So you just saw the Django rest framework, what is mandatory, or you could just clone the project from GitHub.

Oh, I see that as stressful, you know.

So I just prefer using Pip.

So what we just did was to address remote, so I installed apps right when we did it in the settings.

And then we added this URL pattern that I said, the rest framework requires.

So we've done all that we've gone gone through the installation, we've come Yeah, we've seen the results of the API's.

And then let's move straight into Django rest framework serializers.

Now let's talk about serializers in the Django rest framework.

So serializers is a structure of representation that represents a data, we want to return in a JSON format, or are saved in a JSON format.

So we can use this realize that to like transform our Django models into JSON, let's first create a new app, and our Django project, and then we're going to do deeper into serializers.

So right here, in where we start, in my command prompt, I'm just going to opt out of this server that is already running.

And then I'm gonna create a new Python, a new Django app for the smart project.

And we're gonna name it, whatever I want to name it, maybe my app or DRF app or something.

So first of all, is of total t so t should pick gun within a second.

And then yeah, okay.

So, okay, well, that was a lot of press.

So now we can just do Python manager spqr start verse a DRF.

So this is gonna start a new jungle up in our project.

So when this is started, was gonna go ahead and continue with it.

So, first of all, after this is started, we're gonna go into our modules and create a module that we're actually gonna play around with.

So let's just switch we'll just do that.

And that should be done.

Within a second, give you a moment to do and see.

Doesn't take time.

So as that is doing, let me talk more about serializers.

Actually, I wanted to explain more about serializers.

Because we are getting started with the restaurant mock jelly takes a bit of a while to understand what these things actually do.

So the serializer is a big about the lack of form, like a modal form.

Once you have modal, the way you create a form for modal, so you can submit in Django, so you can submit a form, or update or whatever you want to do.

Think of that as a serializer.

So serialize, I just basically the same thing, you have a model, then you have a serializer that you link that model to and then you specify the fields you want to be you want to be in that particular serializer and enjoy GTO views.

So that was the theoretical part.

Now let's dive into protocols.

So right here, we can see that DRF has been created successfully.

Good.

Let's remove that.

So let's come back in now, we have to create a new file and name it serializers dot p y.

Now this serializers dot p y is the file where we're gonna configure our serialization.

For now we're gonna leave this blank.

Now, as I said, we're gonna create a new module that we're going to use.

So let's come into module.pi.

So writing a module does pure let's just create a new module.

I want to name this module like country.

I change my mind on me students are learning from the models.

That's Moodle.

So let's give it some fears.

And Alyssa, the name of the student, that would be muddles towards Sheffield, under character feud should ever maxlend.

Let's just say 100.

Let's give it another one like age.

That's moodle.in teacher fuge.

I don't think that takes any attribute.

Let's give like a description of the student who deals does, it should be a text field.

And that doesn't have any attributes, or let's say date a route.

And that should be a date time field.

So they'll be models dot date, time field, let's say auto now should just be equals to true.

So I think we have these.

Yeah, I'm happy with these.

And so let's just give it a string.

Yep.

And then as do the self on alleges average return, self name.

So now we have this particular module.

Now, you know, normally in Django, when you create a new module, or you make any changes to the modules file, we need to migrate it into our database.

So we're gonna do that right now.

But before we do that, I want us to, first of all register this up in our installed apps.

So coming to sentence under right here, what we're just going to do is to say if that's the name of the app we created, so let's save that.

So now that we have that saved, what we can just do is to come into our command prompt.

And then let's just migrate associate Python managed.

py make migrations.

So we'll give that a second to make the migrations it might see no changes detectors.

So I just like making sure I make migrations and migrate.

So I don't get any errors because sometimes, Django might be funny.

So that should be doing okay.

Yes.

So, as you can see, it says creates models to dent biologists guide and migrate.

So as you can see right here, when we said money, those people I make migrations But what he did was just to make some migration done Moodle student, right here is just migrating every single thing into a database of admin of DRF.

That is, that is what we want to see.

So now that we have done migrated, let's go back into the serializers, that fewer.

So now we need to import the serializers from the rest framework.

And we also need to import the post model from the models.py file.

And after that, we can create a serializer for our post model by specifying only some fields.

So first of all, let's do from rest framework.

Import serializers.

So now this is how we import the serializer.

So we can use it, just like the way you use forms in Django.

From Django dot forms, import forms or something like that.

That's just where we import the serializers.

So let's also import that module in which you want to use so from dots models, imports, students.

No, we have these two things imported, we're just going to first of all, create a serializer class for that student.

So say the class will be student serializer.

And then we're gonna do from the serializers dot model serializers.

So as you can see, it's quite similar to the form where we just have like, if it was a fun one to use, we can say student form, and it will say forms on Moodle, very similar, if you're used to using the Django forms, you definitely definitely understand this.

So knowledge is our class meta, under specify, specify the model loaded with the student, and then the fields.

So the fields, let's make it a total.

Let's see, let's just take two, so we just want the name and then the age.

So these are just the two fields want to use in this serializer.

Now it is that easy to specify a serializer to configure by any word you want to use.

So now let's head back to the views.py file.

So right here, right here, we're gonna import our post serializer and our post model.

So to do that, let's just say from that serializers.

Reports, student.

So I've already made a mistake, I said from the serializers.

I said, we are going to put post serializer.

But we are going to put in post serializers why budget students realize that we actually do have post, but I'm just going to use the same post as like a model, because that's what I use for examples most of the time.

So why wasn't a student serializer right here, class students serializer.

So from serializers, which is DS, please make sure we have that.

From just serializers we're importing the students serializer.

Calm you're good.

So now that we have the students here, imported, let's just import the students model.

So from the models, import students.

So now we also have that imported.

Let's go me and make sure that's correct.

Student Yup, that student.

So now that we have those two things important, let's create a POST method in our test view class.

So in this video class, we're gonna create a POST method.

So we can receive data, just like in a form.

So to do this, we'll just add a function below in artists with class right here.

We'll say def posts.

Now this is going to be a post not to get and then you just gonna take exactly the same thing that should be self.

Exactly the same thing.

And there's going to be self requests.

And then keyword.

So now that we have that was gonna spit for our serializer in a variable.

So this URI lives equals to sudents serializer unreligious give it a data.

So when we do something like data equals to request.

So this is a request towards data.

So when we do something like this, or it means is the, we actually want to use a form or want to submit a detail something like this, we'll say data equals request or data.

So beneath, we're going to see if serializer is valid.

So first of all, check if it's valid.

What we just want to do now is to say serialize, a dot save just exactly the way we do in a Django form, I'm always referencing to that Django form, because it is very similar to this.

I said earlier, if you've worked with Django form, before, you understand what's going on right here.

So serializer data.

So what I just did was, first of all, we specify the serializer.

But our serializer is this student serializer.

So normally, if one was in a post, like, if you don't want to submit a form, you can just remove this as a serialized because students realize what I said, because once you submit something like a form who has to say data equals to request or data it needs to get the data which is being posted into this particular API view.

So let me get that we'll say he does realize that that is valid.

That means if all the values value in blue are correct, just like we want to collect an integer field, of course, we want to have an integer field, if we're supposed to call it an integer field, we have all character food or Tex food, then that serializer is not valid.

So this is just checking you is valid if everything we want, even if values are correct.

And if it's valid, we'll just save it to serializer dot save.

And then we'll just return the response of that particular data, which is awesome.

So you know, just like when you save a data, we just want to show it back to us.

So So data has been saved, this particular data has been saved.

But if these don't happen, we'll just return a response of an error.

Sources serializer dot errors.

So that is it.

So now, let's save this.

And then let's call and just roll our server once again.

So what I want to do now the server is running.

We want to test this particular API in postman.

So let me first of all, wait for that to load up.

So okay, that shouldn't be done.

Okay.

So it says, module not found no module named DRF.

Project serializer.

The one this is saying is that if we come back to our code indirim find a particular.

Okay, good.

So what is the saying is that he didn't find modules, the serializer that was the serializer ndrf proach.

So we're importing this dot model dot serializer.

We're supposed to import it from you.

But as you can see, we were importing it for me.

So normally, we're supposed to use like the views dot view, in our project, actually, we can still use it in.

In this place, no one is was using the app, but because he's in the project, we just need to do is to make sure that we are bringing it in from yours.

Let me just say, from those D, F serializers.

And then from the d r f dot models, so we can just save this and that should work perfectly fine.

So now let's see if our data renewed and then it should run without any errors and good so As we can see, everything runs successfully.

Now, as I said, Let's come back to our browser.

As I said, Now we need to test this API today is an application called postman.

So let me open a new tab and show you to get a smile you'll need to do is type download postman.

And then once you type that you just hit enter such it.

So postman is an application, or software, anything you like to call it, which is used for testing your API.

So since our app is doing production yet, or prigionieri, production is on live on the web or something.

For some testing locally on our locals, we can use this application code customer.

So right here is going to take you to the website, we are going to download the code.

So it's going to automatically detect your voice.

So as you can see, it changed to Windows because I'm on a Windows, download it just installed like a normal app, or the wind installer, most of 2008, you should be good to go.

So now that we have postman installed, I have it open right here.

Well, I just want to do is to create a new tab right here.

And then we're going to test the API in which we just created.

First of all, I'm gonna come in here and then copy.

So I'll copy the URL, because that's the URL I'm gonna be testing anyway.

So right here, I'm just gonna base This is the URL I'm gonna be testing.

And I want it to be a POST method.

So right here, I'm gonna call into body.

So and then I'm going to click on form data.

Now I'm going to input key.

So this key is where I want to submit.

So let's see, we'll come back into our VS code, we come into our serializer, we can see that the fields required a name, and age.

So we know that in our models name is a character field and age is an integer field.

So to avoid any error, we need to make sure that we abide by everything.

So name, does give you the value of admin.

And then key, that is the age range, just give it a value of 25.

Now, if we sent sending the request is our postman works, it's gonna tell the API for you.

And then gauge so as you can see right here, name, it gives us a response of this particular data name, admin, age 25.

But how do we know that this particular data has been submitted into Moodle? So what wants to know is, as this data is really saved in our database, for us to be able to do this, we need to check, we need to open up our admin interface, and set up our admin panel and then check our database.

So let's quickly do that.

First of all, let's go back in here, we're gonna cut out of this server.

So now that we are out of the server, what I want to do is to create a super user, so I'll say python manage.py.

Create super user.

This is going to prompt me to input my user name.

And my email my password, just like I'm registering for a platform or for website does exactly the same thing.

So let's give it a moment to your, so I'm just gonna say admin.

I'm gonna leave this blank.

I'm gonna give it a password.

Yeah, bypass and good.

So now let's run the server.

Again.

That is running, we're gonna come into our VS code.

And in the admin, the Pew II, well, just gonna first of all, import from dot models, import students, and they want to register that student in our admin interface.

So admin, side or register, students.

So now this will show light as we register in admin panel.

We're gonna check that in a minute.

Please make sure this is running Good.

Let's come into our browser as open a new tab.

Let's go to slash admin.

So But as quickly do that right now.

And then login using the admin info.

Okay, good.

So now we can see the under DRF app, we have students, if I click on me, we should have one objects in there.

So as you can see, we have want to dance, which is admin.

And we submitted that from our API.

We didn't create these from this admin panel, or from our projects or from our website.

It's true, our API.

Now let's go test that one more time.

So we have this API, we have the name, alleges give you something like admin two, and then admin two is 27 years old.

So now when I send, because it gives me admin to 27 years old.

Now the reason why it gives me this is because if I come back here in our code, we'll see that I said that, if it's valid, it should save that data.

And it wants to save that data, it should give us a response of serialized data.

So that means if that is successful, the response I want to get is the data in which was submitted.

So if I come back to postman, anytime I get the data I simple idea I submitted it means that was successful.

So let's come into our admin panel, it refresh.

Now we have another one right here.

So that's how to submit from our API.

Now, that is cool.

Now let's talk about serializing, the data that is going out, and I mean, the get method.

First of all, we already have model registered, we already have created a super user.

Now to use our get method, we're gonna come back into our fear school.

Now, what I'm saying is, as you can see, this get method right here, when we did it earlier is just a blank data, just a normal dictionary, by in this post method, in this post function, we are using serialization, let's serializing our data.

So let's say want to also serialize the data in this gate.

Now all we're gonna do, since we already have self request, argument and keyword argument, we're gonna specify a query set.

So let's get rid of these panella silica query sets.

query set can be students, those objects does all know what we can just do because this serializer can be equals to students serializer on the water specified our query sets, and we need to specify one more thing insane.

Many equals true.

Now, the reason we need to specify many equals true is because this query set is a list of objects.

So from this student module, we're getting all the objects I want, what I mean by object is right here, all the data we have in this student database, the student database or the object.

So as you can see, it is more than one is going to bring a list does where we have to specify many equals true items is more than one.

So after specifying the query sets in a variable, I just have another variable named serializer, which is taken from the students serializer.

And I pass in the query said, Alan, I have to tell you that many is equals to true.

Now if it was just one data, but yeah, just one particular object or something we can just put square he said, was since is more than one after many equals to true.

So now that we have that we cannot return a response of serializer data.

So when a user tried to, say this API using a get method, it is going to return a response of dcls other data.

And this is a list of the objects in the student model, which is basically all the data in that site.

So now, let's save it.

And then let's go test our API once again.

But this time around, we're just testing we get so get.

Don't need to remember to remove this first of all, so it doesn't see no knowledge of it sent.

And as you can see, it doesn't give me a crease it lists of the data we have in our project.

So it gives them in a JSON format.

Now if you don't see this, this particular script And then that is just a dictionary.

But since we see these two ever again, and curly braces, which is not taking each of the data, now we know that it's returning it in a JSON format, which is the normal standard way of returning API response or request.

So that is what we can do when we're talking about get serializing.

Our gets request.

But as I said, in Em, I talked about many equals true.

But let's say it's just only one data.

How do we do that? So first of all, since we have this query set, which is all the objects, we know that, oh, God has more than one.

So first of all, let's get one, there's only one of the objects so we can say, student students, one shouldn't be equals to queryset dot first.

Now, what this query said or fails does is that is going to get the first object in these students who do so writing serializer, which students serializer.

And then we remove many dots true.

And voila, passing the query set.

Now, we're just passing only the first student, which is student one.

So now, we can just return a response or serialize the data.

So let's go on to sit once again, writing we're gonna get so once we send, as you can see, just gives us only the first one, the data, we can easily serialize our data in the Django rest framework.

Now let's talk about authentication in the Django rest framework.

So what do I mean by these authentication? Now, these authentication is going to allow us to protect our API endpoint.

So we might have some API that we can just allow anybody to use without authenticating or without authorizing that user.

That's fine.

Boom, I also have some API's, which we want a user to, first of all be authenticated, I want the user to have an API key or a token or something before you can assess that particular API.

Just for example, we use the YouTube Data API, it needs to provide an API key which you get from your Google Cloud account or something like that.

So that API key is going to be used to authorize you or want educate you to be able to use the API.

So let's do something similar in this tutorial.

First of all, what we want to do is to come into our coach, right here.

In views, we're gonna use something called permissions, these permissions is from the rest framework.

So if I say from rest, underscore framework.

That's permissions.

Import.

So when I say the restroom of the permissions inputs, we are going to get a bunch of permissions on bunch of wanted dication lists.

So like I can use allow any.

Now this is quite self explanatory, when clicking allow any just allows any access, this isn't strictly required.

But you could use an empty permission class list just to be.

So what this is just saying is that you can allow anybody to use this particular API.

So let's also see another one like, is authenticated.

So you can see we have is admin user is authenticated, to dedicated or read only.

So we have a bunch of so is admin user does also say for display explanatory doesn't be an admin user before the person can access the API, or is authenticated, this is the one we are going to talk about.

So the user has to be authenticated into this application.

Before we can give the user the chance before you can give the user the way or something to be able to use our API.

So now let's import is authenticated.

This one.

So now that we have that imported, we can use it in our test view.

Plus, to do this, we can just add the code right here.

So let's say before any of the functions, let's just add a variable named permission classes.

Now these permission classes is going to give us like the permissions we want to collect before a user can access any of these.

So most users on dedicated, can anybody just access it, mostly be an admin user, whatever.

So there's gonna be a tuple.

And then we'll say is authenticated want the user to be authenticated? Let's give it like that.

So we only want authenticated.

Now if you want allow any damage anybody, you can also just leave it blank like this.

This just means that anybody can access it.

Or you can just remove that.

Anyhow, you want to do so by using dedicated means the user needs to be on dedicated before, you can use it.

Now let's save it and try something.

If we come into postman, and then we just sent is gonna throw us an error.

So as you can see, okay, this is not the arrow we are looking for.

So this is just saying that our host is not running.

So let's come in here.

So it's running right now.

Let's try that again.

So it's gonna give us a notification arrow.

Good.

So it says detail.

authentication credentials were not provided.

So now as you can see, it requires us to be authenticated to provide some authentication token or something before it can give us access to URLs to our API's.

So now to be able to, to set up this authentication, we need to come back into our code.

And in settings.pi, we're gonna scroll all the way to the bottom.

On the right here, we're gonna say wrists.

All the scuff free work should be equals to column braces.

And then who said defaults.

I'll just go on to education underscore classes.

Now, what we need is a square bracket.

And then you say rest.

Now this should be in small caps, rest, underscore framework.

authentication, token, authentication, this is what we need.

And then now we can save this.

So we can create this file.

And then now that we have now to use this rest framework, we also need to actually add something, let's go back to that settings to the installed apps.

So screw up.

And then in the installed apps, we want to add rest framework.

So as you can see, we already have rest framework added on to this rest framework, dots fourth, talking, so that is different.

And then we'll just save it to read for a month or two.

Okay, now we can quit it.

Before we continue, we need to run migrations.

So let's opt out of this server, Python manage.

py make migrations to after making migrations, no changes detected, actually.

So they're just wanting to spy migrate.

So this is going to migrate to the rest framework dot auth token into our database, which Kathy are talking or talking out.

Okay.

Ledger's roll our server back.

Actually, I don't want to run the server back.

So let's quit it out, I want to do one thing.

So first of all, I want to flush all the data we have in this our database.

So we can, like show up can show you how to create a token for each user.

So now we still have a user, let's flush every single date.

So say my UI flush.

So this is going to flush all the data we have in our database, just click Yes.

And then done.

So now we don't have any user, if I come in here, I should actually be given an error that I'm not dedicated or something.

So okay, it can repeat because there's no running.

Both Normally, the admin is deleted.

Now want to create another super user, a new super user, say python manage.py.

Create a super user.

So right now, it should prompt me to input user name.

So user name, leave that blank.

Why good? So now we have that created.

Now, let's run our server back.

So let's give it So if we come back here now into our server, so it asks us to log in again, because we flushed out the data at first.

So, logging, good to come to the home, you're gonna see now that I have odd talking up, and they Nandita are talking Moodle.

So this is what we just did.

So a good way to create a token for user, one easy way is to just come into our command line is up to the server again, and same Python, manage dot P, y, d, r f, underscore create underscore token, we just pass in the user name.

So this is going to create a token for whatever user name we passing.

So let's wait for that is going to give us a token right now.

And it's also going to save it in the host.

Okay, so as you can see, it gives us this token, just copy that.

And if we come in here, and it's refresh on this model, we're gonna see that we're gonna have one object.

Okay, so let's first of all run our server back, before we go test it out, just to make sure everything is working.

So now let's hit refresh on this token.

Now, you can see now that we have that token right here, the same thing starts with B and with a C, A, B, and with a C, for the admin user and created and this time, so this is what we are, you can easily create a token for a user.

Now this user as a token, let me now show you how you can use this talking in postman, so you can authenticate.

So normally, we're just gonna come into our postman to authenticate using de-stocking, what we just want to do now is to go under authorization.

And then let me first of all, put this down a little bit.

So the type, we want to change this type to API key.

So it's giving me this because I've tested some earlier before.

So let me just show this on step from scratch.

So the key, the default key for Django shouldn't be authorization, authorization, and the value you're going to write is going to be talking and then followed by that particular token, which was created for the user just talking, which is tempting as the stalking.

So let's go back to postman.

So right here, now, we need to make sure that we're adding this to the Edit notes to query parameters.

Let's bring this up a little bit.

And it said, so now, it doesn't tell us now again, that credentials, tightknit some authorization credentials.

As you can see, it gives us a JSON response.

It says name is blank age is not all this is because remember, we flushed our database earlier.

So that's given us know if we have something in our database is obviously going to show that to us.

That's how you can easily authorize or authenticate in your Django rest framework.

Well, now you know that let's say you are using an external framework, like you want to use react or something in this your project, you don't want it to you can't do this manually, like I just saw, we had to come here to type by tomando spy DRF grade token admin, before we were able to generate a token for this user.

We can't do that for everybody on our platform, we want it to be automated.

I mean, that is the main purpose of programming, right? So what we are just going to do is to send details of the particular user, so its username and password are going to send it to our application and is automatically going to generate the token or show us that okay.

So first, we're able to introduce are gonna come into VS code, Amanda URLs A p y, we're gonna say from rest framework.us talking.

Here, this one, those of us want to import obtain of talking.

So what this is going to do is we don't need to create a new view in views of UI.

So this is a default, built in view, which is from the rest framework that are talking.

So it allows us to just send user's credentials, and then it obtains the authentication token for us.

So now let's add a new path.

Let's give a comma, path.

And then right here once the API slash talking, slash, and then it'll give you that obtain, or stoking.

And then I'll give it a name of something like obtain.

So now that we have that, we can test our API.

So first of all, let's copy this.

Let's go back to postman, to what wants to do is to open a new tab.

And then let's set this to post.

And then let's give it slash API token.

And then right here are gonna come into body form data.

And then we're gonna use the key of user name is going to be the user name, which is admin, and then a key of password.

And then this password, I'm gonna leave it blank, because I don't want to show any password in this video.

But once you just put the password of that particular user on it st is going to show you the authentication token for that user, basically.

So that is how you can do the magic also access this from your code, you know, you can do something like send in a request to this particular page, and then getting the authentication token.

So that is how we can easily authenticate a user in Django rest framework.

So I really hope you understood everything we did in this video.

I hope you go through the concepts of the Django rest framework.

I hope you understood API's or Europe you now know about serializers.

Comments:


    More Stories

    Cover Image for Save Content

    Save Content

    Bryan Guner
    Bryan Guner
    Cover Image for JavaScript Basics

    JavaScript Basics

    Bryan Guner
    Bryan Guner