Files
pygeoapi/examples/django/sample_project/README.md
T
Tom Kralidis 199a1fc612 update Django integration (#971)
* update Django integration

* address PR comments
2022-08-30 22:41:48 +02:00

92 lines
2.3 KiB
Markdown

# pygeoapi Django integration
## Overview
Django is a Python web framework that encourages rapid development and clean, pragmatic design.
The pygeoapi and Django integration can be visualized as follows:
> HTTP request <--> Django (`pygeoapi/django_app.py`) <--> pygeoapi API (`pygeoapi/api.py`)
This directory contains a [sample Django project](https://djangoproject.com) demonstrating how to
integrate pygeoapi into your Django application.
In this document we create a sample Django project and use pygeoapi as a pluggable, embedded application.
## Integration pygeoapi with a Django project
To create your Django application from scratch follow these steps:
```bash
# create a project directory and create a fresh virtual environment
python3 -m venv env
cd env
source bin/activate
# install dependencies
pip install Django pygeoapi
# create a Django project
django-admin startproject sampleproject
cd sampleproject
# set pygeoapi environment variables
export PYGEOAPI_CONFIG=`pwd`/pygeoapi-config.yml
export PYGEOAPI_OPENAPI=`pwd`/example-openapi.yml
# Django: collect all static assets/files
python3 manage.py collectstatic
# generate OpenAPI document
pygeoapi openapi generate $PYGEOAPI_CONFIG --output-file $PYGEOAPI_OPENAPI
```
Update `settings.py`:
```python
import os
from pygeoapi.django_app import config
INSTALLED_APPS = [
# other apps
....
#pygeoapi app
'pygeoapi'
]
# Put following setting after STATIC_URL
STATIC_ROOT = os.path.join( BASE_DIR / 'assets')
# Specific pygeoapi setting
PYGEOAPI_CONFIG = config()
...
```
Update `urls.py` to run pygeoapi at e.g. `oapi` path
```python
from django.contrib import admin
from django.urls import path, include
from pygeoapi.django_pygeoapi import urls
urlpatterns = [
path('admin/', admin.site.urls),
path('oapi/', include(urls)) # added here
]
```
Update `pygeoapi-config.yml` as follows:
- set the `server.url` property according to your Django application URL (e.g. in this case the path set is `oapi`)
- set all data paths (e.g. `tests/data/ne_110m_lakes.geojson`) to match with the absolute path of the project directory
Finally, run your Django project:
```bash
python3 manage.py runserver`. Once server starts, head over to `localhost:8000/oapi` to see `pygeoapi` running.
```
At this point you can go your Django / pygeoapi project at `http://localhost:8000/oapi`