Making a GeoDjango App: Introduction & Overview
Thank you for joining me for this Real Python video series where you’ll be making a location-based web application with Django and GeoDjango. The app you’ll build will be a very simple one. You’ll have a data collection of shops in the city of Baltimore. This data will be queried to provide a subset of shops closest to a user’s location.
Let’s go over the list of tools you’ll be using in this tutorial:
-
Python version 3 will be the programming language you’ll use to create the application. You’ll code in a Python virtual environment and use pip to install required packages.
-
You need to store data for this project. This tutorial will walk you through installing PostGreSQL for that purpose. PostGreSQL is a relational database, and you’ll add the PostGIS extension to incorporate spatial data features.
-
For your backend web server, you will install the Django web framework and enable GeoDjango for spatial data support. GeoDjango is included with the Django installation.
GeoDjango will require some drivers for spatially modeling the data. These drivers can be tricky to install individually, so in the interest of time, this tutorial includes an installation of QGIS which is a great open source GIS application that as an added benefit, installs these drivers gracefully.
As an bonus, you’ll use Leaflet to produce a map with markers showing your user in a graphical way the shops that are nearby.
Downloadable Sample Project: This course includes a downloadable sample project that demonstrates the final state of the Django app you’ll build throughout the course. You can get the source code for it here on the materials
repo.
00:00 Hi everybody! Thank you for joining me for this Real Python video series, where you’ll be making a location-based web application with Django and GeoDjango. The app you’ll build will be very simple.
00:14 You’ll have a data collection of shops in the city of Baltimore. This data will be queried to provide a subset of shops closest to a user’s location. Let’s go over the list of tools you’ll be using in this tutorial.
00:28
Python version 3 will be the programming language you’ll use to create the application. You’ll code in a Python virtual environment and use pip
to install required packages. You’ll need to store data for this project.
00:43 This tutorial will show you how to install PostgreSQL for that purpose.
00:48 Postgres is a relational database and you’ll add the PostGIS extension to incorporate spatial data features.
00:56 For your backend web server, you will install the Django web framework and enable GeoDjango for spatial data support. GeoDjango is included with the Django installation.
01:09 GeoDjango will require some drivers for spatially modeling our data. These drivers can be tricky to install individually, so in the interest of time and simplicity, this tutorial includes an installation of QGIS, which is a great open-source GIS application that as an added benefit installs these drivers gracefully. Finally, as a bonus, you’ll use Leaflet to produce a map with markers to show your user in a graphical way the shops that are nearby.
01:40 So, why this stack? For standard web applications, Django and PostgreSQL are great options. Django is one of the two most popular web frameworks for Python at the moment, and PostgreSQL is an established leader in open-source relational database systems.
01:57 The remaining components in this stack provide us with ways to represent and query spatial data, so let’s talk about spatial data for a moment. You’re building an app that lists shops near a user’s location, sorted by distance. You’ve probably seen similar applications.
02:13 It seems simple enough, right? Access the data, query it, and provide the user’s results. After all, if you wanted to see all shops in a city or in a zip code, that’s a fairly straightforward query. The nuance here, however, is that this app sorts by distance and that’s not something easily done with a regular query on a regular database.
02:33 When we try to query based on location, especially relative locations such as “Places within three miles of me,” or inside a boundary, we introduce some complexity. Is that distance a radius?
02:46 Does it take roads, access, and elevation into account? Sure, we could figure out some math related to latitude and longitude coordinates, but in general, if your app depends on geographic location, it’s better to have GIS capabilities in your database.
03:04 GIS stands for Geographic Information System. A GIS integrates data formats and functions for the purpose of collecting, managing, and analyzing spatial data.
03:16 Please join me in the next video, where we’ll set up the necessary components to make our location-based web application. See you there.
Jackie Wilson RP Team on May 5, 2019
Thank you so much for your feedback, Peter. Teaching is one of those professions where you’re always questioning and tweaking your approach/accuracy in a constantly changing field. It’s so nice to receive positive reinforcement, and I’m so grateful you took the time to submit your comments. I’m glad you found value in the videos, and to be fair, the course was loosely adapted from the article by the same name, written by Ahmed Bouchefra in December 2018: realpython.com/location-based-app-with-geodjango-tutorial/ While I did make quite a few changes, his article was the original inspiration. It’s so heartening to know my videos have been helpful. Your comment made my day/week/month…
louisdupre on Sept. 8, 2020
Hi,
Question. I’m using Django for my online website (MySql) with Cpanel. So I don’t use Django on my personnal computer. I got different apps. I’d like to use it for userprofile model. How I can install GEOS GDAL and PROJ.4? Directly by the pip?
I’ve tried with GDAL and I got an error (pip install GDAL).
Traceback (most recent call last):
File "/tmp/pip-build-hw2ao46r/GDAL/setup.py", line 257, in get_gdal_config
return fetch_config(option)
File "/tmp/pip-build-hw2ao46r/GDAL/setup.py", line 154, in fetch_config
raise gdal_config_error(e)
gdal_config_error: [Errno 13] Permission denied: 'gdal-config'
Could not find gdal-config. Make sure you have installed the GDAL native library and development headers.
louisdupre on Sept. 8, 2020
Sorry for posting here button your buttons do not work on others chapters…
Narendrakumar Ratibhai Patel on July 8, 2024
Help me to solve this error
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "D:\pythonProject\geoshops\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "D:\pythonProject\geoshops\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "D:\pythonProject\geoshops\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\pythonProject\geoshops\venv\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "D:\pythonProject\geoshops\venv\lib\site-packages\django\apps\config.py", line 224, in create
import_module(entry)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'shops'
Martin Breuss RP Team on July 8, 2024
@Narendrakumar Ratibhai Patel the error message ModuleNotFoundError: No module named 'shops'
indicates that Django cannot find the module shops
specified in your INSTALLED_APPS
setting.
Here are the steps you can take to resolve this issue:
Check INSTALLED_APPS
in settings.py
:
Ensure that shops
is listed correctly in the INSTALLED_APPS
in your settings.py
file.
INSTALLED_APPS = [
...
'shops',
...
]
Verify the Directory Structure:
Make sure that there is a directory named shops
in your project directory, and it contains an __init__.py
file (this file can be empty). This marks the directory as a Python package.
Your project directory should look something like this:
geoshops/
manage.py
geoshops/
__init__.py
settings.py
urls.py
wsgi.py
shops/
__init__.py
models.py
views.py
...
Check for Typos:
Ensure that there are no typos in the name of the directory or the module in the INSTALLED_APPS
.
Virtual Environment:
Ensure that you are using the correct virtual environment where you installed your project dependencies. Activate your virtual environment and then run your Django project:
$ source venv/bin/activate # On Windows, use venv\Scripts\activate
Check for Python Path Issues:
Sometimes, Python path issues can cause this error. You can try to run the following command to see if Python recognizes the module:
(venv) $ python -m shops
If this command throws an error, it means that Python is not able to find the shops
module in your current environment.
If you have verified all the above and the issue persists, please share more details about your project structure or any recent changes you made that might have caused this issue.
Narendrakumar Ratibhai Patel on July 9, 2024
Thanks for your kind reply. I have gone through all these setup as you mentioned earlier. But, no worries finally I found the solution and the solution is that I have add these two lines to set library path in settings.py
GDAL_LIBRARY_PATH = r’C:\Program Files\QGIS 3.24.1\bin\gdal304.dll’ GEOS_LIBRARY_PATH = r’C:\Program Files\QGIS 3.24.1\bin\geos_c.dll’
Martin Breuss RP Team on July 9, 2024
Hm, interesting. I’m glad you got it to work, but it seems unlikely that these missing settings were causing the ModuleNotFoundError: No module named 'shops'
error that you shared above.
Adding the GDAL_LIBRARY_PATH
and GEOS_LIBRARY_PATH
to your settings.py
is typically related to resolving issues with the geographic libraries (GDAL and GEOS) in Django projects using GeoDjango. Django uses these settings to find the GDAL and GEOS libraries if they are not in a standard location.
Maybe you fixed something else as well on the way? Either way, I’m glad it’s working and congrats on figuring it out 🥳
Narendrakumar Ratibhai Patel on July 14, 2024
Found error while migrating data from JSON file:
File "D:\pythonProject\geoshops\nearbyshops\migrations\0002_auto_20240714_0830.py", line 15, in load_data
objects = json.load(datafile)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 293, in load
return loads(fp.read(),
^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 828: character maps to <undefined>
Solution is:
with open('path/to/your/file.json', 'r', encoding='utf-8') as f:
Add , 'r', encoding='utf-8'
, as shown above.
Martin Breuss RP Team on July 15, 2024
Great, thanks for sharing @Narendrakumar Ratibhai Patel!
Become a Member to join the conversation.
Peter T on May 4, 2019
This is an exceptional course and a extremely professional approach to teaching. It’s obvious to me that Jackie has finely honed her college teaching experience and it shows throughout these videos. Her thought process is completely easy to follow and she doesn’t leave any gaps for the readers to Google the answers for themselves. She has also transposed many of her video words into written words for the readers to peruse. Maybe these will turn into a fully complete written tutorial. (hint) Excellent course!