├── distance_proj ├── __init__.py ├── __pycache__ │ ├── urls.cpython-38.pyc │ ├── wsgi.cpython-38.pyc │ ├── __init__.cpython-38.pyc │ └── settings.cpython-38.pyc ├── asgi.py ├── wsgi.py ├── urls.py └── settings.py ├── measurements ├── migrations │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-38.pyc │ │ └── 0001_initial.cpython-38.pyc │ └── 0001_initial.py ├── __init__.py ├── tests.py ├── __pycache__ │ ├── admin.cpython-38.pyc │ ├── apps.cpython-38.pyc │ ├── forms.cpython-38.pyc │ ├── urls.cpython-38.pyc │ ├── utils.cpython-38.pyc │ ├── views.cpython-38.pyc │ ├── __init__.cpython-38.pyc │ └── models.cpython-38.pyc ├── admin.py ├── apps.py ├── forms.py ├── urls.py ├── models.py ├── utils.py ├── templates │ └── measurements │ │ └── main.html └── views.py ├── .gitignore ├── .DS_Store ├── db.sqlite3 ├── README.md ├── manage.py └── templates └── base.html /distance_proj/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /measurements/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | // geoIP with GeoLite2-City and GeoLite2-Country 2 | geoip/ -------------------------------------------------------------------------------- /measurements/__init__.py: -------------------------------------------------------------------------------- 1 | default_app_config = 'measurements.apps.MeasurementsConfig' -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/.DS_Store -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/db.sqlite3 -------------------------------------------------------------------------------- /measurements/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /distance_proj/__pycache__/urls.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/distance_proj/__pycache__/urls.cpython-38.pyc -------------------------------------------------------------------------------- /distance_proj/__pycache__/wsgi.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/distance_proj/__pycache__/wsgi.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/__pycache__/admin.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/__pycache__/admin.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/__pycache__/apps.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/__pycache__/apps.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/__pycache__/forms.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/__pycache__/forms.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/__pycache__/urls.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/__pycache__/urls.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/__pycache__/utils.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/__pycache__/utils.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/__pycache__/views.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/__pycache__/views.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/__pycache__/models.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/__pycache__/models.cpython-38.pyc -------------------------------------------------------------------------------- /distance_proj/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/distance_proj/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /distance_proj/__pycache__/settings.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/distance_proj/__pycache__/settings.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import Measurement 3 | # Register your models here. 4 | 5 | admin.site.register(Measurement) 6 | -------------------------------------------------------------------------------- /measurements/migrations/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/migrations/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/migrations/__pycache__/0001_initial.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyplane/Django-with-Geolocation/HEAD/measurements/migrations/__pycache__/0001_initial.cpython-38.pyc -------------------------------------------------------------------------------- /measurements/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class MeasurementsConfig(AppConfig): 5 | name = 'measurements' 6 | verbose_name = 'Measurement between 2 locations' 7 | -------------------------------------------------------------------------------- /measurements/forms.py: -------------------------------------------------------------------------------- 1 | from django import forms 2 | from .models import Measurement 3 | 4 | class MeasurementModelForm(forms.ModelForm): 5 | class Meta: 6 | model = Measurement 7 | fields = ('destination',) -------------------------------------------------------------------------------- /measurements/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | from .views import calculate_distance_view 3 | 4 | app_name = 'measurements' 5 | 6 | urlpatterns = [ 7 | path('', calculate_distance_view, name='calaculate-view'), 8 | ] 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django with GeoLocation 2 | GeoLocation and Folium Project with Django. 3 | 4 | Calculate and visualize the distance between the current location and the destination. 5 | 6 |  -------------------------------------------------------------------------------- /distance_proj/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for distance_proj project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'distance_proj.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /distance_proj/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for distance_proj project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'distance_proj.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /measurements/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Measurement(models.Model): 6 | location = models.CharField(max_length=200) 7 | destination = models.CharField(max_length=200) 8 | distance = models.DecimalField(max_digits=10, decimal_places=2) 9 | created = models.DateTimeField(auto_now_add=True) 10 | 11 | def __str__(self): 12 | return f"Distance from {self.location} to {self.destination} is {self.distance} km" 13 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'distance_proj.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /measurements/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.7 on 2020-06-05 11:33 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Measurement', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('location', models.CharField(max_length=200)), 19 | ('destination', models.CharField(max_length=200)), 20 | ('distance', models.DecimalField(decimal_places=2, max_digits=10)), 21 | ('created', models.DateTimeField(auto_now_add=True)), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /measurements/utils.py: -------------------------------------------------------------------------------- 1 | from django.contrib.gis.geoip2 import GeoIP2 2 | 3 | # Helper functions 4 | 5 | def get_ip_address(request): 6 | x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') 7 | if x_forwarded_for: 8 | ip = x_forwarded_for.split(',')[0] 9 | else: 10 | ip = request.META.get('REMOTE_ADDR') 11 | return ip 12 | 13 | def get_geo(ip): 14 | g = GeoIP2() 15 | country = g.country(ip) 16 | city = g.city(ip) 17 | lat, lon = g.lat_lon(ip) 18 | return country, city, lat, lon 19 | 20 | def get_center_coordinates(latA, longA, latB=None, longB=None): 21 | cord = (latA, longA) 22 | if latB: 23 | cord = [(latA+latB)/2, (longA+longB)/2] 24 | return cord 25 | 26 | def get_zoom(distance): 27 | if distance <=100: 28 | return 8 29 | elif distance > 100 and distance <= 5000: 30 | return 4 31 | else: 32 | return 2 -------------------------------------------------------------------------------- /distance_proj/urls.py: -------------------------------------------------------------------------------- 1 | """distance_proj URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.0/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path, include 18 | 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | path('', include('measurements.urls', namespace='measurements')), 22 | ] 23 | -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |