 So I'll be talking on Django and Django REST APIs. So yesterday all the projects were based on what type of projects do we have here, what kind, what is Sunbird, what is collaborative community. So today we'll be talking everything technical. You will see no lines of everything will be coding here. So that's why I want you to all guys to come in front and see so you can see the code center. So how many of you know Django here? No one. So how many of you know Python? How many of you know object oriented Python? How many of you have done more than 200 lines of code in Python? So what is Django? It's basically a framework. So there is built-in features like what is ORM and then there is a template engine, there is a form engine. So you have to basically focus on the code. You don't have to worry about what is how to handle the query sets, how to handle the connection between the MySQL and other things like how to do catchings and all. Those will be done internally by Django. So you have to just focus on how to code and write your business logics in Django itself. So these are the things like ORM. So any of you know what is ORM here? Yes. So objects relational mapping is just, it is basically everything is objects here. So all the database resources will be converted into Python objects. Even in Java also, there are ORMs available. So every framework just follows the ORM. So if you are working on Django, you have to understand what is ORMs, what is objects relational mapping, what are models, what are views, what are templates. So it's basically a model view template. These are the things in Django. What is a model? What is a view? And what is a template? So model is ORM. It's the database. It talks with your database. If database can be any database, you can take MongoDB, Cassandra. I think they don't provide connection for Cassandra, but they provide for MongoDB, PostgreSQL, MySQL. So even you have written a lot of code in ORM and you want to change your database. You don't have to worry. You just have to change the in your settings file. So you have to map. You have to say what kind of database you are using. So you know what is the difference between MVC and MVT. You have heard MVC. So there is Laravel, Codignator. These are all MVC framework. But this Django is a MVT framework. There's no difference. They have changed the name. This view is actually a controller. It's actually a controller and this is the view. But they have changed the name. And they have called it MVT. You will write all your templates in all your template processing in HTML files, all your controllers in view files, all your models in model file. So we'll see now in detail what's the model. So how, what is the execution flow? So this is the execution of entire Django. So when you make a call to an URL, any URL which is there in the system that you have programmed. So all this URL will be mapped to a functions. This function can be a class based function. It can be entirely a class or it can be a user defined function. So all your URLs will be defined in URL.py file. It will map to your view functions which where you will write your actual business logics. So this view functions will call all the models and will render it to a template. So these are all, if you, you have to write all your business logics in view functions. So it can be a class based functions or user defined functions or you have to do some inheritance from all the Django functions that are defined there. Those who have done some thing in Django, they might know what is this. So to create a project, you have to do Django admin start project summary internship. This is a project we'll do today and we'll see how to create interns. We'll fetch some interns, like we'll do some three operations, screwed operations. We'll create some interns, we'll fetch some interns, we'll fetch an individual intern, we'll update an individual intern, throw Django models and throw REST APIs which we'll talk later. So when you will create a project, it will create basically these things. It will create your main repository and then there will be a sub repository where there will be settings files and URL files. These two files you have to just concentrate. Other files, these are admin files you have just by starting the projects to take a backup or other things. In your settings files, we'll define your database settings, your catchy settings, your REST API settings. Every settings related to Django will be defined in setting files and all your URLs, all your routers will be defined in URL files. So whatever functions you will write will have to map to a URL. So those URLs will be defined in URL files. And this is used when we'll, VCV is used when we'll launch our Django in production server, when we'll launch in Django or Apache. Now Django provides a development server internally. So you can do Django managed by run server. It just gives you a temporary server to do your development code. So when you start doing a project, you may divide your projects into sub modules. So if you do summary internship, there will be an institute module, there will be an intern module, there will be teacher's module, there will be a project module. You want to divide your projects into sub modules. You don't want to write all your code in one place, right? So you will start creating apps. So to start an app, you have to do Python 3 managed by startup, your app name. So we'll be creating intern app here. So when as soon as you create an app, it will create a folder. So this is the folder it will create. So every app will have a model file, a view file, can you see? A view file, a test file where you will write your unit test cases, and then an admin file. So Django provides an administration, admin section where you can handle all your admin, all the models you can view in your admin related frontends. In models file, you will write your models, your overings. So every class is a table in your database. Whatever database doesn't matter. So every class will create a table in your database. Every field will create an attribute. We'll create a column in your database. So in case of interns, we are taking three fields, name of four fields, name address created at an institute name. These are the models. After creating our models, you do Python 3 managed by migrations. Why you do make migrations? So then Django can keep track of what are the fields that you have created here. In migration sections, they will keep track of all the models that you have created. Now after that you do Python 3 managed by migrate, and that will create all the database, all the models to your database. You create all the tables, all the database, you don't have to worry about anything. So when you work with models, you don't go to MySQL and change anything or go to MongoDB or change any field because all those changes will keep track, will be keep track on migration sections. You don't change internally. So those are models. So after you have created your models, you need to import in your business logic. Now you will write your business logic, what you want to do with your models. So I want to fetch all the interns, all the interns. Suppose there are interns in the database. I want to just first want to fetch all the interns. I will write, this is a Python function. So I will get all the interns. This is the models, interns, objects, all. I will get all the models. I will put in a variable and I will render with the request, I will render a Python dictionary. You know what is a Python dictionary? I will take all the interns. I will render to intern.html. And now the intern.html will have all the values in the Python dictionary. Now this is the view function. This is just a very basic function. It is not a class based function. It's just a simple function. It takes all the interns and display it in the interns.html. So every view functions accepts a STTP request and gives an STTP response. That's why you have to, in every functions you have to give request. So it accepts a STTP request and gives an STTP response. So the function basically takes interns, objects, all. You are taking all the interns in a variable. So it's a dictionary now. Now when it renders, it renders the interns equal to interns. So it's taking all the interns and sending the interns. This is a variable here and sending all the interns to a dictionary. This is entirely a dictionary. Now in your templates, we'll have a dictionary as an interns. So I will show you how to display it in the templates. So this is the flow you need to do. You need to first create your models. Then you need to define your views. Then you need to map it by URL. So after you have created a, written a class or a function, you have to map it by URL. So this is a regex, a regular expression. So it is a URL name, the view function that you have written, and you have given a name to a URL. And you are importing your views here. So from interns, I'm importing the view file. So after that in the view file, I'll be having all my functions of views dot all interns. So our function name is all intern details. So it is mapped to a particular URL. So when you call this URL, it will give you all the interns in the interns dot html file. So every URL that you will call to the server will be searched in URL patterns. If a URL is not available in URL patterns, it will just given 404. That is the URL is not available. So this is how you will display it in interns dot html file. So this is a template language, Django template language. There are other languages available called JINJA or something else. So Django has a built-in language called Django template language. So this is the syntax. Every conditional syntax will have a percentage sign, bracket and a percentage sign. Every variable will have double bracket sign. So see how now the interns variable will be available in interns dot html because the function is sending, because the function is sending the variable to the templates. Now the intern dot html template will have the interns variable. Now we have to look through it because it is a dictionary function. It will have a lot of values. So we will write a for loop. This is how we will write a for loop. So we will look through the intern variable and each intern will have a name because it is an ORM. Each interns will have a name, an address, an institute and this is how you can display with html tags. We will give some html tags and it can be a table. It can be a list. So this is MVT. Now you can write class based functions. What kind of functions you want? If you can understand this one, this thing, this many thing, this three things. Model, model and then views, URLs and templates. These are the four things you need to know in Django. Now in the previous function we are fetching all the interns. Now we want to get only one intern, not all the interns. You have to write a different business logic or different function for that right. So basically we have to give a parameter to the function which is accepting the. So you have to give the intern id or it is a primary key pk. So you have to give the intern id. You see after request there is a pk. So this function will accept a request and as well as a parameter. Then you will get from interns, objects get that particular intern with pk equal to pk right. To pk equal to pk and it will give you only one intern with that id and after that you render the intern to another profile which is which can be intern.profile.stml. How pk is defined? How pk is defined in the one more thing. So in ORMs if you have not defined explicitly that you want a primary key, it will create a primary key internally for every model. If now you have created name address, created institute. So it will create a primary key internally if you have not defined. So if you will define explicitly for you want to you want your phone number to be a primary key. So you give models.autofield and you set primary key equal to true. There is lots of function available in models and similarly models.foren key you will call other models and will map to those models. So this function is taking n id and is taking the intern and sending it to intern.profile.stml. Now see the URL here. Now we will definitely write a different URL for different functions. So this intern.profile.url this is the regular expression it is taking in the URL and mapping to intern.profile that you can you have to give a name to URL. So this is the regular expression. So this is the it is taking a primary key. It can be 1, it can be 100. So this is the format of the regular expression. There are many other regular expressions available. For example, if there is a slug regular expression available. So if you want to send a name a alphanumeric name to a URL you want to have a different names with the it can be if you want to see an article you want to give the name of the article. So you have to give slug here. So this intern.profile the regular expression and the function. The function will check the will check the URL and find out what field it has. So this pk has to match with this field and similarly in the template file you have to display it since you will have one field only. So you do not have to loop it intern name address. So those are basic Django. Now we will see what is form API. So now we will create some intents and we will use we can use HTML tags we can use and there are some other internal Django form API and model form API available which we will see. Can you see? So there are basically three fields the intern name address and the institute name which will be given as input by the user and this is the function we will write for that to create a intern. So the create intern takes three fields. So http post dot post http dot gate or http has a lot of other fields like this request this http request variable I am talking about this request variable have lot of other fields you will get if you send anything to great you will get request dot gate. So you will basically get three fields from those three fields you take those fields. So intern objects create it will create the intern. So we will create the intern. So after creating the intern you want to view it into a profile or some other. So you will return redirect. So redirect function takes another function and sends it to the give you a parameter here and is rendered to another function. So any confusion if redirect function till here. So how is intern getting created? So it is taking three variables creating the intern. So redirect functions calls other functions. This is another function which we have already created previously. This calling the other function and that function needs a parameter which is the pk it is a primary key takes the intern after creating the intern the intern will have this intern it creates the intern as well as it sends the object that is been created in this variable. So it will take the variable and gives the id to the function. So every functions you create every form you create you have to give the csrf token. So why do you know what is csrf? It is cross site request for jerry attacks there are many kinds of attacks available. So Django keeps you safe from all this you have to just write csrf token. So if in any websites you see for example in wordpress there is a ck editor available if you embed some javascript code or form codes it gives you a alert it gives you some kinds of you can literally attack the site using some javascript code or some some kinds of but Django it protects you from all this just you have to give the csrf token. The csrf token generates a hash code which which is 126 I think or 256. So it hash code it is generated every request every request there it is a different csrf code csrf token. So in every form in every form you have to just write csrf token defined inside the form. So this will redirect and then it will render for create intern I am not showing you the url you definitely know how to map it to a url. So this is that that was how you can create a function a form using html now we will create some form using built-in Django features and that is one is forms.form and one is form.form model model form. So this is form API it will match the number of fields with the models number number of fields in the models you have to create the same number of fields in in your forms. You definitely do not have to create the created by field because that will be created internally by the models itself it will take the machine time and machine. So name address institute this is the forms API it extends the form forms.form. So this is the form API similarly the same code previously we have written create intern. So the what does the form API gives it gives built-in validations. So this form.clean data name address you see these three fields it checks whether it is an integer field whether it is a character field whether it is a Boolean field it checks all that it matches with the models and gives you the value if the it checks it gives you the built-in validation. So if form is valid so when you send a form when you send through a request post request if the method is post. So you have to create the form using this request.post field because there will be three fields and it will create internally using the form these three fields will be available will create the intern and this other things are just the same this is form API there is no difference between that just we will get a built-in validation. So this is the difference you do not have to write the HTML codes here do not have to write any fields you have to just give the form.s paragraph. So there are other tags form.s table you just give the csrf token because the form has all the fields the number of field you will define here there are three fields so it will create three fields for you with the label sorry with the label as name label as address label as institute. So you just have to write form as paragraph got it pretty easy right no one is asking any questions. So that was form now we will see what is model form. So this is less number of code again so you do not have to create the form at all you just map it to which model it is say what model you are taking you are taking the intern model and you want to say how many fields you want to display you will give the number of fields until display in the HTML page you just have to map to model forms when you create the form API this is a model form API you will say which form it is and the number of fields and it will display form as paragraph this is a model form. So in model forms also you do not you have to write very less number of codes you have to just take if the form is valid and form save you have you do not know what is happening internally it is happening internally the form API is handling everything. So basically if the request is posed it is taking all the variable from the post and creating the form it is taking the form is valid saving the form after saving the form it gives you the that object that is being created you call redirect functions which is another function or if the if the request is not posed you create the form send it to the create a steamer and give the form variable there so it will render in the template what is the benefit of one over the other see when you have a team is very small you have two people to work for you will like you want less number of codes to write so definitely the development process becomes faster if you want to write a steamer you can write a steamer and obviously you get the built-in validations you do not have to check whether it is SQL injection or a or a other kind of attacks are there so this form is valid will check everything takes the what is the type of field you are sending if you in a integer field you are sending some query text some SQL query you have sent in the integer field so it will check everything if it is valid then only it will save otherwise it will give you the form so the benefit is obviously you can work hard you write all your codes or you can take the benefit of form API you can always so only thing is that it is happening internally you need to understand it so till now we are writing all function based view now we will write some class based views so there are two types of class based views one is class based and one is generate class based views so in class based views you do not have to do that conditional check if the method is posed or if the method is get you have to just give the sttp method name so you know what are the sttp methods status codes you know what are the status codes you know 404200 ok those kind of the status codes so in a class these are the functions these are the methods you need to write and it has to match with the sttp methods so the rest of the thing are just same this gate will give you just when you make a gate request to this class will give you the you will come to this gate function will make a post request will come to the post function and you write your post logic in the post function will get write your gate logic in the gate function there will be other functions you can add like delete function update function those function name are similar to sttp methods name and you have to write in in the it extends the jango views generic in class so it has built in class function built in methods you can also override those function you can define other functions here which are there in the view classes then those you can see from the documentation obviously so basically you have to give the name of the functions see there are other functions available render function you can write you can write the in the post function you can define the render function and after the gate is done you call the render function you after the post season you can call the render function so when you map a URL to a class you have to give as class name as view so when you map it to a function you just write view in the function name so when you map it to a class just have to give the views class name as view and you definitely have to give the name because the URL template text will search with the name not with this that was class based view in class based views you just have to give the functions name what are available in sttp methods so what is generic class based views it is just like what we have done in model forms in model forms we have just map it directly to the model in generic class based views these are the classes available and will map directly to the models so what is happening internally will not get anything so this is the this will give you the list of interns so when you write a generic class based view gcbv so you extend the class write which model it is it is a context object name so what is a context object name so when it will it will thus all the gate posts everything internally because it is extending the list view so in the template intern interned or stml it the interns will be available in the form of interns so that is why you have to give the context object name as interns take a time to you are not getting any question you do not have to write any post any gate obviously the post is not allowed in this because it is a list function list view it only it is a gate method is allowed here post method is not allowed because you will obviously want some interns so that is why you are using list view right so it will say that which model it is it is an intern model okay then what is the class it will see you see the list view it will get all the interns internally so which variable what is the variable name you want in that templates you say this intern and then it will render directly to the template this is list view let us see how you can create a intern using the generic class based view you see the very less number of code you are do not have to write any post function any gate function you just have to say with and also what method is allowed here the post method is allowed here because it is a create view so you say which model it is you will say intern model you have all you also have to define the form class this new intern which is a form api because you want to have form in the stml so this is a new intern form which I have already defined before this is a model form and you give the template name so after the intern is has been created so it just it is a success URL so see I do not want you to understand this now because everything is happening internally so if you understand the basic function it is good but just think that something called generic class based view is there so tomorrow you go and see what is it if you can understand it is very good because everything is happening internally don't have to do just have to write four lines of code and your intern is created before in the class you have to write 15 to 20 lines of code for that so you just have to define the model name form class template so what is the detail view so detail view if you want to see a particular instant of that in last last view was giving the list of interns and you want to get only one intern right you say similarly the model name the template name the contest object it will take the value that will send in the url patch the value with the class will patch the primary key from the url and return that single object so this is detail view there are other update view is available and lot of other views are available so that was gcv i don't want to go more in details if you have any question you can ask me so if you can understand just url templates and views that's fine if you can understand class based view that's great if you can understand generic class based views then you have to do in very less number of code you can write a lot of things so you like only machine learning so how many of you know machine learning here only supervised learning or unsupervised learning what kind of learning there all the models unsupervised also great great how many projects have you done in machine learning three projects good you can also mention in the description your project that you have done in machine learning maybe you will get a chance in machine learning topics but i would like to say that there are many other fields other than machine learning obviously machine learning is i know it's going on but in the industry if you go and give an interview they will ask you the basic things do you know object oriented the basic things you need to know like how many of you know aws here aws you know how to create clouds you know how to you know what is aws lambda you know what is platform as a service you know what is platform as a service right cloud company you know what is function as a service so aws lambda lambda is a function as a service but it is not machine learning see there are lot of other things wide number of topics other than machine learning which you can learn obviously you should learn machine learning i am not saying you should but the thing is that these are the basic things like rest apis will talk now you need to understand those kind of things machine learning is good in machine learning you have to write class based views function based views which i am talking you will obviously have to write some open up some rest apis in machine learning also because these are the basic things need to learn how to read and write so if you write a machine learning code and your you have write all written all class based function in machine learning let's talk machine learning now because you all like machine learning suppose i don't know machine learning but you guys know machine learning so but you have give written the machine learning code now you want to open up your machine learning models through rest apis now you don't know rest apis how you will open up without knowing the rest apis you open up the machine learning obviously you have to learn my rest apis so you know what is rest apis how many of you know what is rest apis what is rest apis so why do we need rest apis you know the uber architecture and you have written or have read the uber architecture what is micro services before like before the weather company used to develop some software they used to write the entire architecture in one language one platform using java in one platform or you may in a particular platform you may write two in two three language but how do you communicate between two three languages you need definitely a language which can understand a common medium so you definitely need rest apis for communication only the software need to understand sttp so in uber many micro services are written in go many are written in python many are written in java to take any language i'm not saying you have to write everything in python so you have to you can choose any language but you need to make some communication between them so you need to create some rest api for them for communications and obviously each of those languages need to understand sttp but right so the communication happens to sttp and what is the value that that is transferred that is that can be an example that can be a jason which is transferred between through rest apis it is language independent because you can choose any language and create your rest apis so we will be seeing how to implement a rest api using using jango resprint mode so till now we have seen what is class wise views what is model functions and just similar with very few lines of code you can create a rest api okay let's see so it's a browsable api i will show you a live i'll show in my web in my website if i can access from here what is the rest api how i have implemented some rest api i'll show you what is browsable api literally it's just you can browse your rest api through a built-in feature there are many kinds of authentication available now you have written some code in python you have written some code in java now you want to make a single authentication for example when you log in to a uber service they have lot of payment service gateway service other kind of service available but you have login just once single authentication so that authentication can be token base it can be session base usually it is token base there are many kinds of token base authentication available like just ortho you can go through it ortho ortho so there is only single authentication which authenticates all the apis so there can be some apis written in python some apis written in java and there is single authentication which are handled by the rest apis and serialization so what is serialization so now your model can talk with the database so database objects are different model objects are python variables now if you send your python to java the java cannot understand the serializer does is that it takes the model variables and converts into python objects it can be dictionary it can be list anything then serializer converts all those variables into json format you can convert your variables into the XML format also then you send it to other you can send it to java platform you can send it to go platform because every platform under does understand json so you know what is json what is json is javascript object notation so what is a serializer it converts your models objects into json converts your json object into models serialize the serialize ok and obviously it is a model serializer so you are mapping directly to your model so this is an intern model and what feels you want to open up to an api you want to give your intern id your name address and institute so you have to mention here the fields the serializer will convert the model instance into json format with those apis so this is a class base i will show you how to do in functions also just i am showing because we have just now learned what is class base apis what is gcb so but it is a serializer so when you think of serializer just think of form apis in rest apis serializers are just like forms form apis model form apis this is a model from serializer that is it these are the two lines to create an api so this is the class base function we will say what which is the model and what when you write query set instead of model you can you can just display the model in different formats for example you want to order by name you can just write other functions to give a query set in different format for example if you want to just get three interns out of hundred interns i want to just give three interns at a time through my rest apis just you can write other functions in your query sets so you can also give instead of writing the query set you can also write models here so your models can be is intern your serializer is the serializer that you have created last and then you just and then you map your serializer to an api api means the url name you can give any name and the mapping is just the serializer and the or the class and the as view so that's it that's just four lines of code for creating an api but i guess it may be hard to understand what is happening internally but it's just like gcv generic class base views so you create a serializer you create a function this will be created in view sets so view sets why i'm i'm calling it view set because in basic jango you call it view so in serializer you can call it view set so how do you create a serializer in which where you will create your well where you will write your apis you can write it with your jango function but that will mess up all the code so you can create a separate file in each of the models you can create a separate file called view set file usage dot py and write all your views all your view sets there for example this is the view set intern view sets so it will it is a model serializer so it will give all the interns and it will connect it to the internet right so i will just show you how a serializer works so this is a collaborative community project so you see this is the name of our project is collaboration system so when you create a project it will always create a sub folder with the same project name which is collaboration system all others are just apps that you or modules you have created separately so in collaborative community we have a resource called basic article we have a community app we have a group app it's just like stack overflow where and you can also say as a group based thing where we have created so in the user related roles my mission we have captain user related roles NC value which is another thing so you see for basic article what we have done so every app will have a models a view file and a view sets in view sets you will write your api so we have written all our views here all our models here our serializers will come here you see this is the serializer i'm talking about there is only one serializer now the article serializer it will give me all the articles only three lines of code it will say that this model is articles and the fields id title and body i'm taking i'm saying that this is the i want all the articles and this is my serializer and if i just open up it's giving me all the articles through my apis it's giving me the id title and body through my apis so now this apis not authenticated anyone can access this api because i'm not provided permission classes because anyone will access and get all my articles from any part of the world using any language so how to do permission classes how to write apis we see that but i think you can understand what is api how to write an api you basically write your serializer which will map to the models and then you will write your view sets and you open up an api you give you all the api in JSON format now you can write an ajax query in jQuery and you can get all this in any series so this is where we have included some apis here so when we this is a course module that we have so every course module will have list of topics and every topics will have links so in each of these topics there are certain resources are there so we are patching all those resources through apis so these are the resources so if you click here it will it has only one resource there are three resources so this api just looks like this so i'm getting three videos now so this is the api so i'm patching with an id i'm getting all the resources so this is basically a browsable api so you can create api this browser you will it is built in feature in tango press framework and you can patch all those using ajax function so it is written here course in course now you can either create using a serializer file here now you see there is no serializer and view sets file in this module because i have separated in the api folder so you can also either write that way or this way your apis you create another folder api and you create these four files serializer serializer and views so in serializer you will define all your serializers and view sets and this is the url so you see these urls and the main urls are totally different so it extends the main url so you can either declare all your urls in the main url by file or in every models every modules you can define a url file and you can extend the url file using import url getting anything not getting anything something something okay then let's not see the rest of the code because it is going very only code code let's not see what is function how to write a function you want to see or don't want don't want to see yes i know that okay because everything is code now it's going very technical i don't want that to be happened but it's okay you can come and talk to me anytime and i'll just explain you what is happening we'll literally see it and write some code and many of you will not be doing jango also maybe some of you will writing note js code so the thing is going internally similar note js might not have this internally built-in classes for apis so do you want to learn what is jango there is a resource called simple is better than complex you can note it down it starts from very basics you can also search in your google but simple is better than complex to start from basics and it will explain everything what is glassways views it will definitely not explain what is rest apis because rest api will not find any resource after learning gcbv and cvv you can come to me and i will talk about rest apis because rest apis very going out of so basically you just learn these three things okay and we'll talk later one more thing if you are creating multiple instance of jango in a single machine so you need to create a virtual environment for that you you install virtually and be in your machine and you can run multiple instance of jango so your models maybe some jango might might be running in another version you may write multiple versions for example the latest latest version is 2. something but in our project we are using 1.11 which is the lts version so i prefer lts so you should also go for lts but they might have changed some syntax in 2.0 so if you want to run multiple instances you just install the virtual environment and you will find lots of documentation for that okay thank you