How to build a GeoDjango app | Learn Django GeoLocation with Folium (Part 1/3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys in this tutorial series we are going to create this very nice-looking project which will allow us to calculate the distance between two locations so currently I'm somewhere in California and if I would like to go for example to New York I just need to type in New York to check the distance so let's press confirm and as you can see I have a distance from my location to New York which is over 4,000 kilometers now if I would like to go to Dortmund let's press confirm and as you can see the distance is over 9,000 kilometers so this is something that we will do in this video series I think it will consist of three parts and in the first part which is this one we are going to create our model based on the model we will create a form then we will also create a viewer we will import our form and we will display everything in the template and in the next video we will start working with the deal location stuff and volume okay so yeah without further ado let's get started alright guys so Before we jump into coding let's just have a short overview on what have I done so far so basically I didn't do much I just started a Django project which I called distance prod and then I renamed the distance barrage directory to SRC and now inside the SRC I have a distance prod folder which is our main project folder and here among others we have settings py file and URLs py file which we will modify in this project and then next to the distance approach we have measurements application where we will work on the models py shortly okay so let's just have a look at settings py here I added measurements so I added this application to the installed apps and also I told Django where to look for the templates okay so this is basically it and I didn't do anything else so yeah without further ado let's continue working on this project let's jump into otto's py file and over here we are going to define a class which we will call measurement so basically every time a form gets submitted we will store some data and we want to have a location field so this is going to be where we currently are this is going to be models char field and we will get this from our IP address okay so I'm going to put in max length is equal to 200 and then we would like to have a destination so where do you want to go and this is going to be exactly the same so models char field all right and now we can put in the distance and this is going to be a decimal field with max digits 10 and the same applies is 2 okay and let's add a created so this is going to be a date/time field the eight-time filled with out now add is equal to true okay so this way we will know where we when we are submitted a form we will keep the track of when we created a record in this created field all right so finally the one thing missing is the string representation so we can put itself and let's return well we can put in distance from and then let's put it self location to self destination is and then we just need to use this self distance self distance and at the end let's add kilometers okay so now we can hence a bliss and let's register this measurement in the admin so we will be able to yeah to display it in our admin so over here we need to import our model from dot models import measurement and then we need to write admin site register measurement alright and now let's go to the terminal stop the server from running Python manage py make migrations Python manage py migrate and Python manage py run server so now let's have a look at our admin we should be able to log in I also created a super user so I didn't mention this before but I created a super user and here is our measurements application we can actually change the name so maybe we can put in over here measurement between two locations or something like that so how can we do this we can jump into our apps py and over here we just need to provide a verbose name and let's just put in measurement between two locations ok and now we can save this and we can copy this measurements conflict because we need to provide it in the init B Y so here we just need to write down default app config and then we just need to indicate that we need to go to measurements and then apps and use this measurement config so now if we save it go back and refresh here is measurement between two locations alright so now let's add a record and I'm going to type in New York and I want to go to Dortmund and the distance is 9000 kilometers so let me save it and here it is here is our string representation so distance from New York to Dartmouth is 9,000 kilometers all right so this is working and yeah we can jump back into visual studio code and inside the measurements we are going to create a new file which we will call forms py so now we need to create a form that will allow us to get the destination okay so we want to have a form where we can type in the destination so the location will be as mentioned before we will get it from the IP then the destination needs to be provided by the form and in the background we will calculate the distance okay and then the created date is simply going to be set automatically all right so first of all we need to import forms from Django import forms and then we can create a class measurement model form and this inherits from forms dot model form model form and with the class meta we need to indicate what model we are referring to so we actually need to import our model so from dot models import and we want to import measurement and here we need to put in measurement and we need to specify the fields that we want to display so I'm going to open up a couple and I'm just going to put in destination okay and don't forget about the comma so we want one field okay because again the location will be set from the IP address the destination is the field that is interesting for us so we need to get it through the form the distance will be calculated automatically and the Creator is set up automatically all right so yeah just don't forget to save this form and we can go to our views py file and over here let's create a function view which we will call calculate distance view and this will take in a request and the first thing that we want to do just for now is to display the object that we created a second ago okay so in order to do that I'm going to create another variable called obj and yeah we need two more imports so the first one will be get object or 404 and the second one will be the model so from models import measurement and now I can use this get object or 404 and pass in the measurement and since the object that we created is the first object we just need to specify that the ID is equal to 1 ok and the next thing is to pass this obj to our template we don't have templates yet but we will create them in just a second so I'm going to create a contacts dictionary where the key will be the name that we will use in the template so in this case I'm going to name it distance and to the distance I'm going to assign value of obj all right and finally I'm going to return render the first parameter the parameter will be the request the second one will be the template so in our case it will be measurements and main HTML I'm going to return to this in just a second and finally what we are passing to this template and we are passing this context dictionary okay so we just need to write down context all right so this should be working let's go ahead and save it and let's create this measurements main HTML so first of all we need to go to measurements and over here we need to create a folder not measurements but templates templates and then inside the templates we are going to create this measurements folder and inside the measurements we need to put in this main HTML why do we do it like this well imagine that you have many applications and you want to have many main HTML files to avoid confusion you just put in the application name inside the templates and then you specify the files so to be more precise if we go to the views py here we have the measurements and this is this directory inside the templates folder and then we have the main HTML all right so what we need to do next is to create another templates folder inside the SRC directory and here we will put in base HTML and this templates directory is what we defined in our settings py file so over here we can keep some additional files related to our project one of them is space HTML maybe another one can be for example navbar HTML may be home HTML and so on so in case of base HTML well this is related to a very important and powerful feature of Django called template inheritance so we can specify one template and we will do it in a second with the help of bootstrap from which other templates will inherit from and we won't need to rewrite the code over and over again we will simply define some unique blocks some unique places where we will provide some unique content so for example if we do a contact page we will put in some unique content related to the contact page so for example we will change the title to contact us for example and in the content in the body we will put in contact form if we do an about page we will simply put in the title about us and in the body we will simply have some kind of a description alright so yeah without further ado let's jump into bootstrap over here if you go to get bootstrap comm documentation you will find this starter template you just need to scroll down a little bit and simply copy everything from here and paste it inside the base HTML so we want to have a common title for every template that will inherit from this base HTML and this will be G or Django but next to it we will define a block title and the next place where we want to define and this unique block is the body so over here let me just put in block and this will be a block content but I would also like to wrap it up with a div class container so this is a bootstrap class and this will add a had some spacing on the left and on the right so let me close up this diff hyungs yeah we have it basically ready so one more thing is to get rid of this slim because I don't think the model which we will add a little bit later we'll work with the slim version and I'm just going to delete the integrity and cross origin over here and yeah we have our solution ready so in this project this template inheritance isn't a big deal we will probably have only one view however if we decide to extend this project well we will have a great starting starting point because we created our base HTML and we have this functionality of template inheritance all right so let's jump into main HTML and now in order to inherit from base HTML we something need to write down extends base HTML and then we just need to define the block title first and here we will have calculate calculate distance so it will be geo Django calculate distance okay and block content and here we are going to display the this distance which we are passing with this context dictionary to our main HTML so let's just put in over here this all right so we can go ahead and save it and there's actually one thing missing we need to link it up with the URLs so over here what I'm going to do is to head over to our main project folder and in the URLs py file in the main URL spew I file I'm going to do an import include and we are going to set a new path and this is going to be the main path and next to it I'm going to use this include and now what I need to include is to go to the measurements and then in the measurements in a second we are going to create a URLs py files so I'm just going to put in URLs and need to define a namespace which is going to be the title of the application so I'm going to put in measure men's measurements okay so now what we need to do is to provide this URLs in the measurements application so let me do it very quickly URLs py and over here the first thing that I'm going to import is from Django URLs import path and then the second thing is to get our view so that from that views import calculate distance view and then I'm going to set an app name which should match the name space and this is going to be measurements and then URL patterns again we need to set a path and again we want to have it as the main path so we need to keep it as blank and then the second parameter is this calculate distance view and the name will be calculate view all right so let's save this and let's also be sure to save the main URLs py file and yet now we can test this out so let's go to the browser and here it is here is our object distance from New York to Dortmund is 9,000 kilometers okay so the next thing is to display a form over here so we need to go to our views inside the measurements application and we need to import our form so from dot forms forms import and this was measurement model form alright alright so over here what I'm going to do is to set a variable and instantiate this measurement model form and I'm going to pass in request post or none and we can simply pass this form into the template by creating another item in our dictionary and this time I'm going to set set the name as form and this is the name that we will use in the template so this is the key and the value will be the form over here ok and now we can save this file go into main HTML and let's add a horizontal line and over here let's write form action it's equal to blank and then the method will be equal to post and let's also add outer complete is equal to off so we won't have we won't see the history of our form submits and let's close the form and let's add a CSRF token since we are dealing with a post request and we will need the form itself so here we are putting this form and we also need a button to submit this form so button it needs to be type equals to submit and to let's add a bootstrap class let's make it a blue button so BTN BTN primary and let's just put in a con firm and so let's close off the button so now if we save this go to our website here is our form it's not looking nice but we will customize it a little bit later what we can do over here is to write as P for now and this will be as paragraph so that the confirm button in this case went down but we will install Django krispies forms so this will look nicer okay so now that we have our form we can continue working on the logic and the views so over here we can write down if form is valid and if the form is valid we are going to set another variable called instance and this will be formed save commit equals to false so what we are telling Django right now that we don't want to save the form just yet because we only get information about this destination field okay and we need to provide some information about the location about distance and the created will be set automatically so this is what will come through the form this is something that we need to set right now later as mentioned before it will be set automatically and the same will go for the distance we will do some calculations in the background and set this automatically but for now we are going to use some fixed values so going back to the views I'm going to begin with I can write down instance and then destination and this will be just form cleaned data get the the nation so this is what's coming through the form and then instance and the location can be for example San Francisco alright and the instance distance will be I don't know five thousand kilometers and this is a decimal field so I can't do it like this I need to do it like this all right instance distance all right so yeah finally we can simply write instance dot save and this should be working so let's save it and let's test this out let me refresh the page let me put something in like New York let's press confirm and let's go to our Django administration's measurements and here it is here is our object so this is working and yet as a final thing for this video let's install Krispies forms and let's have this form look nicer and in order to do that we need to go to the terminal stop the server from running pip install and then Django Chris be crispy forms let's wait a few seconds okay and then we need to go to the settings py file and add crispy forms to the installed app list and finally we need to also set the crisply template back so I'm going to do it maybe somewhere over here crispy template back and this will be good for okay so now we can go ahead and save the settings py I didn't make hopefully any typos bootstrapped for and we can go to our main HTML here beneath the extents let's do load and we don't want to load static but we want to simply load krispies forms tags okay and over here we are going to delete this SP and we are going to use Krispies filter going to save it refresh and we have some issues okay because we didn't run the server Python managed py run server let's try it out again and there it is here is our form alright guys so we will finish off over here in the next video we will start to do some cool stuff related to geolocation and volume if you like this video please subscribe to my channel if it was too basic for you yeah it will get more interesting in the next one I promise once again thank you and see you soon take care and bye bye
Info
Channel: Pyplane
Views: 31,434
Rating: undefined out of 5
Keywords: geodjango, geolocation django, folium, geolocation, learn django, python folium, folium python, python maps with folium, python folium interactive map, python folium map, python folium real time, python folium leaflet, python folium distance, python maps geo, python google maps, dynamic maps python, django 3, python interactive maps, folium library in python, python folium django, folium python map, folium leaflet python, django 3 tutorial, python maps api, Python maps
Id: _KIMevaubfQ
Channel Id: undefined
Length: 26min 47sec (1607 seconds)
Published: Wed Jun 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.