Files
pygeoapi/examples/django/sample_project
Benjamin Webb e1af5e1ca5 Add json-ld templating for feature collection item (#868)
* Render template if specified in config and provide example config

Update linked_data.py

Merge branch 'geopython:master' into jsonld

Rended from json-ld output

Render jinja2 json-ld template from json-ld output instead of json output

Merge branch 'geopython:master' into jsonld

Add documentation

- Add documentation
- Add test to workflow

Update pygeoapi-test-config.yml

Update test_api.py

Update api.py

Update linked_data.py

Move template declaration in configuration

Update docs

Update configuration.rst

Update configuration.rst

* Updates per requested changes

* Fix spelling

* Fix json-ld template pathing

* Remove  root path for the templating

* Move json-ld template from api.py

- Move single item json-ld templating to inside geojson2jsonld
- Reformat json-ld configuration for context and item_template to children of json-ld block
- Update docs and example configurations

* Fix ref

* Use FileSystemLoader to control template search path

search for templates is in order of `template_paths` list

* s/json-ld/linked-data/ig

rename json-ld to more generic name
2023-02-15 21:12:19 -05:00
..
2022-08-30 22:41:48 +02:00
2022-08-30 22:41:48 +02:00

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 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:


# 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:


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


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:

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