How to connect Django Project to Multiple Database Management Systems (Part2/2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is kenneth braney from cambridgetech and welcome back to the channel so in this second part of the video we are going to build an interface for the front end and see how we can make database queries and also add data from the front end so what i'm going to do is i'm going to go back into vs code and inside of settings.pi and just a quick recap this is where we've included our database engines for the multiple database connectivity now let's go into our templates over here and let's specify a base directory for a template so i'm going to do base underscore drl and i'll do a for a slash and i'll type in templates so this template is going to be the name of a folder i'm going to create and all that you are trying to do is we are trying to set up our systems like that you are going to have a place where we are going to store all our templates so i'm going to create the folder and i'll type in templates for the name and inside of templates this is what i normally prefer to do so inside of template i'm going to create um subfolders and these subfolders are going to correspond to the names of the apps we have over here so right over here we have the school app and we have the club app so i'm going to create one for club and i'll click on this once again and i'll create one for school then i'll click on it again then i'll create one for partials all right so partials are going to hold our partials template and the phase of it is going to be the base.html you see me do this a number of times in other videos and i'll create one for let's say enough dot html all right so the concepts you are trying to do over here is we are going to use template inheritance and tons of that sort so let me go back into our browser and go to getbootstrap.com because you're going to use the bootstrap theme and like i always do i prefer to use a cdn so i'll click on all releases and i'll choose version 4.6 alright so in here with a cdn i'll just click on this starter template and copy it and inside my base.html i'll paste this over here then let me just change this to let's say your home page and let me just clear this and i'm going to add the navbar over here but as you can see the now.html is here now let's go back into bootstrap go to component and inside of the component you have the number you can select from any of the options over here so i'm just going to select this one i'll click on copy and come and paste this in number all right so so in here we are going to use template inheritance and what i'll do is now that we have this we can just put in this template tag and i'm going to say include and i'll bring the quotation mark over here and this is going to be in partials for slash nav html that enough but we fitted at the top here all right so we need to go into um the apps themselves and we need to creating some views so let's say we have the school view over here so i'm going to pass in a request and you are going to return render and the first parameter i need to pass in here is the request and the name of the template so for the template is going to be in school i mean the school sub folder inside of templates as we have it over here and i'm going to create a template i'm going to call screw.html right so inside of this i'll just create school dot html good and let me come back into the views yeah so now that i have this let me create in a url route over here so let me click on this and do urls dot pi so i'm going to see from jungle dot urls i want to import path and i'll see from the current directory i want to import the views then i'll do url patterns and this is going to be a list so i'm going to call the path method over here and what i'm going to do is i'm just going to leave this one blank and i can now have access to a school function and i'll give it a related name for the url and i'll say school all right so i think this is good to go now now all i need to do is to come back to school and um find i'm in the template and see extends and i'll type in partials for slash b's dot html i'll see this and um finally i need to go into the projects for that so there's a multiple dbms and inside of the urls.pi first of all i need to bring out the include method over here then i'll creating a path and i'm going to call include and what i want to include for now is the school go to urls all right later on you're going to bring in the club but it's just going to be a repetition of what we are doing now so just about doing this if i'm to go back yeah okay this was the starter template jungle gave me if i'm to refresh now we are gonna have what we just did and let me just come back here and change these lights themes to dark so when i save this and come here now we are good now let me change these two links okay so the home the home i'm going to say school and the link is going to be clap i'll just leave the others there so that we can proceed very quickly so you have school and then we have club so what i want to do now is when i click on school we should have like the data entry for all the things we have in the school database showing up over here let's go into the school into the school views over here now we want to run some queries so i'm going to import the models over here so i'm going to see from the current models i want to import and the model is cool because there's a name we gave this database model over here all right so back to this now you can see um smaller screw is equal to this screw dot objects dots or now let's try and do this and let's pass in a context dictionary where we have a key value pair so our key is cool and the value is cool now let's pass in let's pass in the context dictionary over here so that you can have access to it in school.html so once again let me come in here so as you can see we have name and address so let's come into the school.html and let's just put in a blog content so i'll do block content and down end block over here then what i'm going to do is first of all let me just copy this block content and come to base dot html come and paste this over here so that's we can easily work with it alright so let me just put in some bootstrap classes so that we can have things look a little bit nice so we are using the container we're calling the row class and the call say call md call md8 and let's use an offset of um let's say true so that we have it in the middle okay so let's put in our template tag over here and this is going to be our for loop so i'm going to say for school in school and let's end our for loop over here i'll say n4 and [Music] let's try and access the data over here so i'm going to say school dot name all right so let's do this and if we are to come back here and come and refresh we have the university of ghana right because if we should go into school that's the only entry we have over here now let's add another one so the name is um mca university diversity of mines and technology and this is in soccer so when i save this we have university of mice and technology when i refresh we have this green up over here so now let's put out a break tag over here right so you have this lined up now let's try and put in um a form over here where we can add entries into our database base ourselves from the front end and this is where we'll be required to be very specific with the kind of models that we are handling so let me do this quickly let me do six let me maintain the eight over here and instead of an offset i'm just gonna have another call so i'll do a call md4 so that would take the first part of the 12 and within this call i'm going to have a card and i'm going to have a card body then i'm going to have a form over here so let me just put in the form tag and let me have a method method of post and for now let me just have my inputs type submit over here and i'll say save as a value now put in a bootstrap class of bt and btn and i'll see info all right so good so i'm just going to have this showing up over here let me with this rule margin top five all right so this is what i want now let's go and create i think i used the container class container okay so i made a mistake over here good all right so this is what i wanted let me try and do this shadow over here all right so now we have this clean up nicely for us um let me also come in here and into this just let me just put up a style over here and with this style i just want to see the body i want everything to pop up very well so i'll do a background and i'll use an ash color so triple c as a hexadecimal color code for ash so when i refresh we have this like this so that's this will show up okay so now let's go and create our forms and see how we work with it so inside our school app um i'm going to creating a new file i'm calling this forms.pi so i'll quickly say from jungle i want to import forms and i'll see from the current models i want to import school and i'll creating a class and i'll call this school form and school form is going to inherit from forms dot model form then i have a class meta like this and the model i want to create this phone for is the school model and the fields it's going to be everything i have over there so it's going to be a double underscore or double underscore just like this all right so now let me import the forms in my view over here and i can see from the current forms i want to import the screw form like this inside of this i can see the form is going to be equal to the school form i'll do [Music] request dots post actually i've covered a number of these things in my other videos that's where i'm going very fast but even before then i'll say if request dot post if request dot method actually is equal to post and that's where we want to have this form rendered else we just want to have a form [Music] in its raw states something like this all right then i'll i'm going to say if on dots i'm going to have the is valid method called over here and i'm going to say form let's save all right then let's return redirect you don't have a redirect imported over here so let's come in here and report redirect so return redirect we just want to return to the homepage remember that school.html is actually the home page because if you should go into the urls you can see that the school is the home page so we don't have any problem over there good now let's go into our school html over here and first of all we need to pass in our csrf token so csrf and that's called token and let's put in the form over here so the form is here and if i'm to refresh you don't seem to have the form showing up over here okay and that's because we had to pass it in the context all right so let's put a comma over here and bring in the form the form is not going to show up over here good so now let's add in another school and i'll see let me see coming chroma and these are all universities in ghana coming from my university of science and technology all right the address is kumasi so when i click on save you have i mean common university of science and technology good and it is showing up over here it's very simple all right so now we are done with the school app now i want to leave the club up as an assignment okay so you definitely have to do it yourself but then the process is going to be the same so the main catch is you will just have to come into the nav.html and work on this url route over here okay i'll leave a video in the description where you see how to put out the links in the jungle way so basically the links um if you want to do that for school school is actually going to be this url name be used over here and if you should come into nav.html all you need to do over here is to put in the template tag and inside here you are going to say url and the name is going to be school all right so when i save this and come back here and come and refresh now when i click on this it takes me to school all right so that's basically what you are going to do for lab after which you creating the model i think the model has already been created exactly so you're creating the form and you're writing the query inside of the views of pi just um just replicating what i did for school so the source code for the entire project will be on github and in that particular one i'll also use django crispy forms to style up everything over here i want you to do it yourself so that you can learn on the go you make mistakes you correct them yourself and that's the best way of learning all right thank you very much now if you find this tutorial very interesting there are a couple of ways you can help me grow my channel and please subscribe to cambridgetech and don't forget to hit on the notification button so that anytime i release the video you'll be really notified also share this video with friends and family who find this content very useful at cambridgetech you say lem programming you can do it and also don't forget to ask any question if you do have one thank you very much and bye bye
Info
Channel: KenBroTech
Views: 527
Rating: undefined out of 5
Keywords: Django Models, Django Multiple Database, SQLite Django, Postgresql Django, MySQL Django, How to connect Django Project to Multiple Database Management Systems (Part1/2), sql, django models, django apps, django project
Id: 8C5AgdrU-NI
Channel Id: undefined
Length: 20min 53sec (1253 seconds)
Published: Mon Sep 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.