diff --git a/nearbyshops/nearbyshops/settings.py b/nearbyshops/nearbyshops/settings.py index 666c931483..168af4d369 100644 --- a/nearbyshops/nearbyshops/settings.py +++ b/nearbyshops/nearbyshops/settings.py @@ -119,8 +119,6 @@ USE_I18N = True -USE_L10N = True - USE_TZ = True @@ -128,3 +126,5 @@ # https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = "/static/" + +DEFAULT_AUTO_FIELD = "django.db.models.AutoField" diff --git a/nearbyshops/nearbyshops/urls.py b/nearbyshops/nearbyshops/urls.py index 78f22415a8..b9cb52b870 100644 --- a/nearbyshops/nearbyshops/urls.py +++ b/nearbyshops/nearbyshops/urls.py @@ -1,7 +1,7 @@ """nearbyshops URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/2.1/topics/http/urls/ + https://docs.djangoproject.com/en/6.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views @@ -18,4 +18,7 @@ from django.urls import path from shops import views -urlpatterns = [path("", views.Home.as_view()), path("admin/", admin.site.urls)] +urlpatterns = [ + path("admin/", admin.site.urls), + path("", views.Home.as_view()), +] diff --git a/nearbyshops/readme.md b/nearbyshops/readme.md index 16da6aef89..6fc30da43a 100644 --- a/nearbyshops/readme.md +++ b/nearbyshops/readme.md @@ -1,6 +1,25 @@ # Make a Location-Based Web App With Django and GeoDjango -This folder contains the bonus material & example projects for our [Make a Location-Based Web App With Django and GeoDjango](https://realpython.com/location-based-app-with-geodjango-tutorial) tutorial on Real Python. +This folder contains the bonus material & example projects for our [Make a Location-Based Web App With Django and GeoDjango](https://realpython.com/location-based-app-with-geodjango-tutorial/) tutorial on Real Python. -Check out [Make a Location-Based Web App With Django and GeoDjango](https://realpython.com/location-based-app-with-geodjango-tutorial/) for more information. +Check out [Make a Location-Based Web App With Django and GeoDjango](https://realpython.com/location-based-app-with-geodjango-tutorial/) for more information. +## Requirements + +- Python 3.12 or later +- GDAL, GEOS, and PROJ installed on your system +- A PostgreSQL database with the PostGIS extension, for example: + +```console +$ docker run --name=postgis -d -e POSTGRES_USER=user001 \ + -e POSTGRES_PASS=123456789 -e POSTGRES_DBNAME=gis \ + -p 5432:5432 kartoza/postgis:18-3.6 +``` + +Install the Python dependencies, then load the data and run the server: + +```console +$ python -m pip install -r requirements.txt +$ python manage.py migrate +$ python manage.py runserver +``` diff --git a/nearbyshops/requirements.txt b/nearbyshops/requirements.txt new file mode 100644 index 0000000000..74e841907d --- /dev/null +++ b/nearbyshops/requirements.txt @@ -0,0 +1,2 @@ +django==6.1.4 +psycopg2-binary==2.9.13 diff --git a/nearbyshops/shops/admin.py b/nearbyshops/shops/admin.py index 53666b29ad..a09d92239f 100644 --- a/nearbyshops/shops/admin.py +++ b/nearbyshops/shops/admin.py @@ -1,11 +1,11 @@ # Register your models here. from django.contrib import admin -from django.contrib.gis.admin import OSMGeoAdmin +from django.contrib.gis.admin import GISModelAdmin from .models import Shop @admin.register(Shop) -class ShopAdmin(OSMGeoAdmin): +class ShopAdmin(GISModelAdmin): list_display = ("name", "location") diff --git a/nearbyshops/shops/migrations/0001_initial.py b/nearbyshops/shops/migrations/0001_initial.py index 0595ee8dc9..85421fa7e7 100644 --- a/nearbyshops/shops/migrations/0001_initial.py +++ b/nearbyshops/shops/migrations/0001_initial.py @@ -19,7 +19,7 @@ class Migration(migrations.Migration): primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=70)), + ('name', models.CharField(max_length=100)), ('location', django.contrib.gis.db.models.fields.PointField(srid=4326)), ('address', models.CharField(max_length=100)), diff --git a/nearbyshops/shops/migrations/0002_auto_20181020_0450.py b/nearbyshops/shops/migrations/0002_auto_20181020_0450.py index b8e87ebd49..a490c3145c 100644 --- a/nearbyshops/shops/migrations/0002_auto_20181020_0450.py +++ b/nearbyshops/shops/migrations/0002_auto_20181020_0450.py @@ -6,25 +6,25 @@ from django.contrib.gis.geos import fromstr from django.db import migrations -DATA_FILENAME = 'data.json' +DATA_FILENAME = "data.json" def load_data(apps, schema_editor): - Shop = apps.get_model('shops', 'Shop') + Shop = apps.get_model("shops", "Shop") jsonfile = Path(__file__).parents[2] / DATA_FILENAME with open(str(jsonfile)) as datafile: objects = json.load(datafile) - for obj in objects['elements']: + for obj in objects["elements"]: try: - objType = obj['type'] - if objType == 'node': - tags = obj['tags'] - name = tags.get('name', 'no-name') - longitude = obj.get('lon', 0) - latitude = obj.get('lat', 0) + objType = obj["type"] + if objType == "node": + tags = obj["tags"] + name = tags.get("name", "no-name") + longitude = obj.get("lon", 0) + latitude = obj.get("lat", 0) location = fromstr( - f'POINT({longitude} {latitude})', srid=4326 + f"POINT({longitude} {latitude})", srid=4326 ) Shop(name=name, location=location).save() except KeyError: @@ -34,7 +34,7 @@ def load_data(apps, schema_editor): class Migration(migrations.Migration): dependencies = [ - ('shops', '0001_initial'), + ("shops", "0001_initial"), ] operations = [