Create, Read, Update & Delete Crud Operations In Django Complete Practical Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to Unique channel in this tutorial you're going to learn about crude operation so now let me just demonstrate what we're going to learn this is just a form of registring students and display them for like we have a full name of the student have feel like the full name a department so now let's just inut some field now I will input let's say s let me inut SAR SAR Mar s camar then her age is 17 and Department let's information system and let's say telephone telephone 12 okay so s great successfully so when you view you see s c so now I want to edit s cam here I will just edit seral camera instead of ser camera I will put um l c yeah like this then her age is 25 for example 25 yeah then updates you say successful student updat successful then I want to delete I will delete Lina then also I want to delete this name I will delete this name so this is what we're going to do throughout this video so now let's get started [Music] welcome to Unique channel in this tutorial you're going to learn about uh crude operation in Jungle first what is jungle jungle is a web framework which is written in Python programming language and also enable us to create web app as fastly or as quickly as possible so now in this tutorial we read operation then also the the the the the operation the update and delete operation so now let's get started first let's create a folder on the desktop here let's right click and create a folder then this folder let's call it a c then right click on the folder here and uh open this folder with a visual studio code but one thing here you have to take notice of is uh you have to ensure that you have python install jungle and also Visual Studio install in your in your machine so now let's let's go now before we go to the particular aspect we have to add some amount of extension now click on the extension here now here let install jungle there then also you can install Auto close tab then you can install auto rename tag and also the last thing here you will you have to install is this boost five and because we have to make use of it and then we have to use that temp the boot template so now let's create our first project to create a project P jangle what we have to do here let's go at the top there click on the terminal then on the terminal here right down here you type um our Command to create app so the command there to create a particular app is uh you can type uh jood D admin start project to create a project so now cre this project oh yeah this will have an error because we don't specify we don't give the project name so now let's go so now here you say jungo admin start project then you give the project name here now this project name let's give it as code c c c so now we have the project already here as C so now let's change directory so let's say CD go to code then after that let's create our app so we say python if you are using Windows machine you can just type py or you can type it in full as python so now let's just sa python manage manage.py start app then let's give this app as a c app so now our app is now created here our app now is created so now when you click here this folder you will see a code app that we just created and within this C project you can see set can URL you can see other things there so now let's run our app now to run our app so what you have to do here to run the app what you have to do you just type python manage.py run server now our server is now running okay wait our server to run okay now our server is now running what we have to do here let's open This Server let's open the so now you just hold the control key and click then automatically this will take us to the default to the homepage for this our project so now when you when you create a particular app or project for the first time then you open that project this is what will appear so now so here what are we to do now let's do amount of configuration what are some of the configuration we have to do here first we have to install our app because we have to create a database for that particular app so see that is the name of our app the name of our app here isore app yeah now to install this app we have to come to the install app section here then within here you can you can type the name of your app so you can do it anywhere at the top Center or anywhere so then after that in our base directory here let's uh create a template so now let's extend our templates here so here we say base director Ty base di directory say command and say template so now here now we have our templates now what are we to do let's just copy this one and let's create our template so now what is template template there has to do with what HTML file where we can store all our HTML file so now to create a template what are we to do we have to go to the create app or the cude app or the app that you created so now let's just click on this folder then you type template TMS then and click enter so now we are through with this configuration later we will create all our template so now we are through with this so after that now let's go to our model let's go to our model now in jungle jungle always uh having this concept of uh mvt that is a model view template the there has to do with the backing or the database aspect of the project or the app then the view there has to do with the logical aspect where we can do all our logical operation then the last thing there has to do with template which has to do with the the view or like the user view of the project or let's say the the front end of the project so now here let's go to our model so now this model here we want to create a student registration Now to create a student registration under the model what are we to do we have to create a class so now we call this class A student now this students class here in the this students class represent a table in the database so whenever you create a class in model here that class there just represent a table a single table in a particular database so here we say model model model model model so after that here we can put column then we specify m o now model models so here now what's are the field that you want to enter for the student name so we say full name that's called name then we say equal to the model there dots the character character field have has to do with what you want to enter in the the the the the front end or what want the the the the database or the table to accept the kind of characters that we want it could be um it could be integer F email or whatsoever any type of f so now maximum length let's give it 255 characters then the full name then let's say the age of the students there is equal to model do DS model. integer field there then also we want to store the um departmenta Department of the student say model character fil then say maximum length there max length l e n GTA length there is equal to 255 characters then the department then telephone telephone there let's give it m character field then let's give it a length of let's say 15 okay 15 so we close then also Let's uh want to start like uh the registration date or the date the student ENT in the University or the college or school so we dat entry dat ENT r y entry date here is equal to model model dot let's say dat dat field and let's say date fi yes then date fi there and uh let just duplicate hold the shift in Windows shift alternate and down arrow then we say end date e n end date then the last thing there we have to store is uh date registered date registered r a s now let's give it uh model do let's say date and time fill yes I want to track the date time and even the the date time the Year this registration took place so so now we see now filo Dash now underscore add so we say it's equal to true it's equal to True CU we want to add this thing automatically so now we have this students created so let's just give it death but although like we have to make use of uh let's just give this death as uh stream that's in the next tutorial I will I explained what uh this is for so now let's return let's return the full self s. the full name of the student then and Let's uh conate plus there want to return self. the department of this particular student so now we are through with um this model here now let's just um move this model into our database here to our database so now to move it to the database what are you to do here let's just create new terminal so let's CD CD cude yeah now we have to run our migration because we want to migrate this class or this student table into the database so now what are we to do say python manage. POI make migrations then we have already you can see that create model students so the students model has created well when you view the SQL light you will not see it here you not see it here what are you to do let's run our myig now if you want to view your SQL database SQL database on B Studio you just have to uh add the extension here just type SQL V just type SQL light you will see it there you will see it uh look at it escale light V you see it automatically installed so now what are we to do here we run our M migrate so say python manage.py migrate migr so now we want to migrate everything of [Music] this now when you will refresh the SQL light you will see all the table migrat then there we can click here the student table then you see everything about you see the full name age well you might ask why ID yeah in Jungle here when you take a look here we don't create any ID fi because of that jungle will assign a default ID for us so don't worry about ID jungle will automatically assign a default ID for you so now we are through our model the next thing here we have to do is to create a form now this tutorial we not create a form we will not use form but we use model form model form here has to do with work everything about the database will automatically generated in that particular form so now to create a model form what are we to do go to the create app or within your app just let's create a new file then call it as form f r Ms form. py So within this form. first we have to import things first we have to import from dot have to import models then have to import everything about this model then from jungo now import forms forms now we have these things imported so the last thing here we have to do is uh we have to create a form class to say class here then let's call it our student form form then within here let's pass our form forms. model form model form then within this model form we create a class Meta Meta class then here we want to collect the form that we created from our model this one student form so here what are we to do just a model here is equal to the student this one yeah this student you see when you when you import a particular package of file when you want to call that then that that package or file automatically you have access to it so now what are we to do here so now want to return or want to access all the fields of this student form so save fi is equal to all so because of all not individual so let's just saycore all like this and put that in a stram format so now we are through with this form so now we are through with the m which has to do with the the model now let's go to our here first here we have to create a a custom URL already when you create a jungle project for the first time you have a URL which is this one but let's create our own URL so now to create your own URL go to the C app or the app that you created within there just type urls.py so now we have this URLs now I want to link this urls.py to the custom this one to our default urm so what are you to do here let's say you type PA then hope we leave this one as blank say include type include function then the name of the app now we include first this app within this app we have a URL so we say c c app that is the name of the app dot call the URL within this app that's why you see the dot call URL do app. URL now we are seeing here include is shouting or jungle is shouting saying include is not defined all what you have to do with import just say include that's it c c include so now automatically now include this what settled so now let's close this yeah now let's um create our templates here first want to create a three templates first we have to create this create students so we say create or add students let's say Okay create students students HTML as I explained earlier that this template has to do with the HTML aspect so say create cre student d n ml then then we have update update then we have read R students. HTML so now we have this one now you might ask what about delete delete no need for us to create a delete and template so now let's go so we have to close this let's close this one now so this template that we created here we have to create view for them then we link our URL so now what are we to do let's go to our view here let's create each and every template function for each and every template here first here we have to first here what are we to do we say def here now first we have the create student we say create that's students then pass our request here then within this request here let's just let's just leave it as pass I'll let's just return return render then within this render pass our request then say command within this request we say create student HL so now we are through with this one here then what are we to do just let's just duplicate three times yeah so here now instead of create students you see what upate student read students read students then let's return this template read student read student then here we say update student update student then let's return update student here this template here that's we cre update student update student so now we have all these things now so now if you take a look here it is not still working if we refresh here you not see any result why because this one here lack URL so what are we to do here now let's link our URL first URL here what are we to do first we have to include things like part say from Jungle PJ a n o jungle. URLs import first I have to import our path import path then within this path now what are we to do see URL patterns here then within this pattern we can able to access our path then let's leave this blank because this one has to do with the home page you can specify anything leave it as blank for now say command then say view then we call the the the function that we created this one we call them to uh view so now first we have this create student create student but we cannot able to access it until we Import in our view so save from dot import view so then here we can now able to access every uh method of function every function that we created from The View well first we created say views here we say command first views dots create student see have create student then we name it as what create students here okay so also we go say command here let just dlic it here then also we have what read students now for here the path here is so let's say read students then say read student also here we have read student then the last one they ask to do is create students copy it there copy there create students you can copy can contr plus C then we duplicate this one here you say create student is there as great students then also say great student so now here if you now refresh your browser here automatically you will not see this default page hereo what we see we see what the homepage of today now let's see something let just type uh create now create Here Also let's type read uh okay let's type read read then update updates here now here let just browse through them I refresh you see cre now let say slash now once to see read e aore students you see read then see upat then you see op let's go to The Logical aspect first to create a student first what are we to do we go to our view which are to do The Logical aspect within this view function here now let's pass our logical aspect first we have to create a let's say variable create students here n now I want to query through this one you say create student want to uh collect want to create this student here using our form already what we to do we call our form well we cannot able to call our form until we import things first let's import two main things first first let's say from uh import view no from that model here let's import all our model there then form forms now import all import all so now within here we call our form what's the name of form student form here can see it's there student form this let's close this this student form that we created here call it student form now the student form then we say request there pass our request which is equal to post now we now we come to The Logical aspect now we say if let's say create students here create students that this variable here if this one here create students if this create students here that is what is valid that we handle our for that we call form validation let's validate our form if this create student do is valid what are we to do now let's just give it try try C or try exception so now we say try and we say create student save I want to save the form then when the form is saved want to throw out a message there that the form has what successfully save so what are we to say say messages. success pass a success message then we pass our request here R request then say student successfully so now we have this but here we having warning see message is not defined all what you have to do import say from [Music] jungle. cont import message yes messages so here within this messages now after the form is successfully save what are we to do want to return this this the user want to return the user to the create form again to the to the place where he she can create the form so say redirect redirect R redirect the individual to the home so leave it as this but still we have redirect is not defined all what you have to do after the render just type direct like this so now after seeing after the try then we pass our Excel then just leave it as pass like this pass so now we have this one but this is not the end for the creates now we have to create our context we pass our context here we say context is equal to now pass our context dictionary then we pass the variable that we created here is student form this context we have to make using in the form let's say create pass then yeah we pass the context we return the context here so command pass return the context so now here when you take a look still when we refresh the browser for the for the O here we are not seeing anything so now let's just display this uh form here decreas students so now let's just make use of our let's make use of our bootstrap now first here let's come and select language mode change this language to HTML then hold shift exclamation of shift number one for Windows then select bootstrap then within this bootstrap we let just say student record students record so now let's give it a boost class now when you go to refresh you see create students record let's give it a bootstrap class say here say class here say aler Aller Das info class then enter the text text Das Center then after that refresh you see there so the other thing here we have to do is to create our form just say form because we are using form model so we don't have to design our form already it's automatically generated so now we say um say CS csrf token this will uh help you help you to protect you from any form of forgery so you can read more about csrf in the documentation so now let's just say csrf token then pass the form pass the form tag that is the from the variable that from the our context dictionary here I just copy create student I will say create student dot as what paragraph now as P paragraph when you refresh here you will see everything about the form automatically imported so here you can able to create file so now let's just give it as now we say input let's say type and we say this is sub button submit give it a class of BNB TN Dash success yes then so refresh so now we can see that we have a success so what are we to do here let's just give an example full name of students C is C is c i l i a this is then say then we see age and let's say 29 say Department information system the phone number phone number here then ENT date let's say she enter this date then she will end this date now click on submit automatically the form has been submitted successfully but what are you seeing here if you take a look here the form is submitted but where at the top of what the browser no we don't want this form here to be to be seen here so what are we to do let just be if this form here is uh submitted and let's go to our SQL lights and click on students so now you will see ASP so you see so instead of submitted to the browser uh to the form then it's submitted to the browser we don't want it so now the reason being that if you take a look here whenever you are submitting form Ure that the method of the form is specified now if you take a look here the method specif the method is said post or let's say uh get so yeah let's just say post meod say method under the form say post so now let's just refresh when you let's enter again to let's say unique full name there then AG is let's say 29 Department let's say programming and let's just say telephone and the entry dat let's give it a random date any date any dat random gr 37 then you see so now because here we specify it as post so you can say no data sent on the browser so now in the inform the method there we have post have gets have any other method but post gets they are more used but when you are dealing with form please always ensure that you use the post method so now let's go now we go so here what are we to do here now we have our success uh a success form so if you go and view your SQL and view you can see that we must have this data entered you see programming can see it there so now let's uh create a success let's pass our success message because already we passed it we created success message so now let's pass it here so now to pass your success message what are you to do let's come here at the top let's pass it there we say pass it there we look through we have to look through this one say for let's say message message in what in this messages in this messages then we end for end four now within here we pass our message tag let's say let's say p i just pass here message message our message so now message is uh let's now let's refresh and I can see student created successfully this one that we past here student created successfully so now we have deal with we are through with our creates now let's go to to read let's go to the read now just uh give it a button first let's give it a button here let's give it a tag then this button will direct us to the read student then to the read student give it a class of warning BTN BT n then let's give it a name of few student student yes so when you click there it should take us to the read student let's go to the read student function so here we only query from our database so now we say read we create a a variable of read is students is equal to from we read the students from not from the form but from the database or from the model so we say students then we say student model like this one or this one that we created here then student model do object all one to query all then the last thing there pass our context pass context then within this context we see read students pleas pass thisum student then first our context dictionary and that we return it here so now we are through with this now all what we have to do now we have to read this through out the students now to read throughout let's just create our language setting language setting so let's see read student read students so now this read students let's give it Ino let's give it a class of info then Center the text to Center the text there now here we create a table because we want to represent all this data in form of table want to display them in form of table so now we say table then we use bootstrap table class and give it a class of what table now within this table what are we to stud we have table letter within this table letter I have to St the student ID so just say ID okay then after that want to store the ID the full name the agent department so say idate one this one full name age then Department then Department telephone date enter [Music] Department telephone then this answering dat and and also admission date instead of dat entry just say admission the admit and dism can let just say date entering date entering then here will say date end dates and then dat register want to also display the date this event was registered or the student record was register say dat a register so now if you take a look here when you refresh you see everything display now like this but this still the records are not display so now let's display them so display them Let's uh create our table row and within this table row here or before creating our table row let's look through this table so now to look just pass the for look I say for what what is the um the Contex dictionary that we pass here is read student yeah say for for read students in what in read students this one for students in R student then we end our for yeah N4 so now within this N4 have table row yeah a table row so let's just have table row So within this table row there we have table data table data then this table data here we we have to pass every data of this table or this our our model class yeah this the column of this model class so now what's our model our student class so what are we to do here we pass the T this so pass say read students here is read student Dot want to first you have to pass the ID me refresh you see ID first record number one then just duplicate then first the ID then we pass the full name Pass the full name there Pass the full name then after passing the full name also we have to pass the age and Department copy this one then I'll say age and Department there age then here we also pass the department telephone Department Department there then then telephone let's duplicate it there t a l e p h o n after telephone we have uh this entered and end dat date entered let's duplicate this one end date instead of date enter end date end date then the last one there we have to it to register end it oh okay let registered this registered Sor okay this register so now last thing there has to do with the action either individual should able to delete or to to update now we say action here let's add one column here say action then under here table let's add update then the last action there is delet now let's design this one say class class of BTN BTN running then BTN danger BTN BTN Das danger like this so now it's all fine yes it's all fine you see so now we see now we can a able to now we can able to enter detail let's add a let's say 25 our department information system telephon dat enter give this one and ending date 2027 submit see have success then r view then we have this so now then man so now here we are now through with our crates and we can read our file then the last two there has to do with the update and delete in ID give it an in instead of string you can put string but in is more okay for me so now here we pass this ID then let's create let's go to The Logical aspect now here pass our ID here so now to read to update first we have to we have to uh query from the database then also we want to store every aspect of uh what we we we create from the database on the form then we can able to edit and save it so now what are we to do here we say update student is equal to then we see from student do object now this time around we are not going to um use uh all then here we have we want to use get because we want to get every value through the primary key or the ID so say ID is equal to ID like this so ID is equal to this ID so also we want this one also to display in Thea so we say update and say student form I put it like this now is equal to First the student form here the instance is equal to this student update student instance this update student this update students so now okay update students double so update students so now here what are we to do condition we say if this request if this request do method here Das request. method I me a method here is equal toal to what post say update form here we say update form is equal to the student update form the student form pass request there C to post do Post then pass our instance which is what this update student with update student then we pass our condition we say if this update student is valid this update student if the data there are all valid what are we to do we pass it try exception we say update student form do save want to save this students form then after saving that student form we want to show a particular message let just uh copy this message here want to show a particular message that this students updated successfully so say updated successfully then we return and redirect the individual to our read student template par students template students template then after that so after that we say accept then let's give it a pass and Def pass so then yeah now pass our context dictionary n t e x here is equal to context dictionary here we say pass our contact dictionary by saying this form first pass this one pass it here then say then put command then also we pass this form there students form then put command like this now here we pass our context TX pass our context so we pass our context now let's just go to our for let's see if it is working no it is not so what are we to do here we go to the update form go to the update form there go to our update form let's just still change our language to then shift exclamation then select this one so here we say update students update students s a n then give it a class of BTN BTN Dash uh aler aler aler alert Das info then uh text Center n r Tex Center text Center still not working so the last thing here what are we to do we pass create our form here sa form then pass the method which is post now we pass our csrf CSR csrf csrf and token then within here pass our tag which is uh this one pass this form tag here so we can see form student. as yeah like this so we give it a submit button we can say input type input type give it a submit then let's design this class of VN Das success success button can give it the value of of of update update then now when you click this F it is not working why it is not working because we have to pass our URL to this one to this is update so now our URL here is uh first our URL is update students so we pass it here that create then pass our key that we want to update which is uh the primary key so pass this one here say the read students here dot ID want to pass the ID of that particular r student now we have an error student got unexpected keyword argument instance yes so what are we to do here when you go right down it automatically show you where the error is update to the uh form then the student class here then we see pass the instance which is update that is line 34 yeah um that's what we have we call Pro pring programming small uh small results then many many bxs many many box to fix so like you have to fixing box so now let's just go out once more and watch the line update students that this query is from our model then pass the object gets in the ID there so of this students form thisable then is equal to the students for here supposed to be student form that's from one this is from the part this is from the form class that we created this one supposed to be from there yes this student form instead of students here so what are we to do let's just correct this one and uh sorry they just say student form okay so now let's go and run again let's see let's run again and see if it has fixed okay yeah it has fixed now so now you can see that now everything is okay we fix whatsoever we want to fix so now let's just go back again now let's just click on Emmanuel now instead of full name uel let just say unique like this then let just edit update the students click on update so now we can see it is now updating man unique so now we have deal with this update uh deal with create deal with rate then we can able now to update our form so but since here we are update we have no update message so what are we to do let's all give it a update successful message and to do that let's go to our updates here then let's pass our message here then we see give it let's look through we say for say for message in messages then end for and four here then within this let's pass a P tag and pass a message message here then let's just can see let's update again instead of 29 let's put 25 let's see okay it is not still sh up let me message in me messages as oh Triple S supposed to be 1 s message okay so now let's refresh the update let say 20 then suppose okay 20 let's go to our view yeah message messages say okay message redirect to St read student it's supposed to give us this message okay so now what are we to do here it's supposed to give us but here we pass this message here to the read student instead of up the student because we want this one to only appear under this three student when the message is successful so let's you have to cut that line of code and P it here here then let just give your refresh again then you see student updated successfully now say let's update umu umu here then you say instead of 25 we say 20 age then our phone number let's update our phone number then click update there you see students updated successfully delete function or the delete functionality to handle the delete functionality what are we to do we have to create a URL for delete just duplicate this yeah then we say delete students d e l e t e delete students here then still the ID you want to use this ID then uh let's say delete student delete student so now here we say delete student we created function say F and the student delete students call it as delete student then under this delay students here in this delay student we say request pass our request then command the ID so here what are we to do here we have to now to delete a file delete a file directly from the database and not from uh the form so we have to we have to uh delete it from the database so like we have to query it first from the database and delete it so now we use our students class so now we can say um delete student students is equal to from the our database which is delete here dot Vera object dot we use get then we pass our ID which is now is equal to the ID so now it's done so we say delete students now we say delete students dot delete function then after the student has been deleted what are we to do we want to send a successful message success message so yeah let's just copy this line of code then we say student deleted successfully deleted successfully then after the student is successfully deleted we want to show still The View student template so we say we return and redirect the user to the um direct the user to delete to read yes to read student to read student so then let's let's go to read students and pass our IDs here yeah first we have to say delete pass our URL delete student student delete student then pass right side here that is this one Rel student here copy it here Dot the ID that we pass that we pass here which is this one or this ID that we passed so now let's that we passed here so now let's see what is happening here now let's just click on delete click on delete you see student deleted successfully then click on this delete also students so have any students if also when you go and view your database you will notice that there is no student there is no students left so now let's add student so let's just add students here let's add Abdul Kamar then let's give it 35 then Department information system then date enter and then still add students Also let's say I need black age is 45 45 or let's say age is 32 Department Information Technology telephone this one then dat enter let say the 6th of the 6 month 2020 20 and the 7th of 7th month 2027 so now successful then see then we see can say now we can see Abdu camar what we enter then instead of Abdu camar I want to update or edit it to abubakar Kamar so I will just say A K A then I say I'll click on update now here instead of a new black key I will just update and give it uh James m a m m mommy then click upit so now we have this so now this is the end of our tutorial if you like this tutorial please don't forget to comment to like and uh um and share thank you for watching
Info
Channel: UNIQUE CHANNEL
Views: 16
Rating: undefined out of 5
Keywords:
Id: nU2wU3DqK08
Channel Id: undefined
Length: 67min 35sec (4055 seconds)
Published: Fri Jun 07 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.