Skip to main content

Add FAIR signpostings to response headers in Django

Project description

Python package

FAIR signposting middleware for Django

django_signposting is a Django middleware library that facilitates the addition of FAIR signposting headers to HTTP responses. This middleware helps in making your data more FAIR (Findable, accessible, interoperable, reuseable) by embedding signposting headers in responses, guiding clients to relevant resources linked to the response content.

Based on the Signposting library.

Features

  • Automatically adds signposting headers to HTTP responses.
  • Signposts can be added manually or automatically be parsed from JSON-LD/schema.org
  • Supports multiple relation types with optional media type specification.
  • Easily integrable with existing Django applications.

Installation

pip install django_signposting

Usage

Automatic parsing of JSON-LD

To enable automatic parsing of JSON-LD, add the following middleware classes to your Django project's MIDDLEWARE setting in settings.py:

MIDDLEWARE = [
    ...,
    'django_signposting.middleware.SignpostingMiddleware',
    'django_signposting.middleware.JsonLdSignpostingParserMiddleware',
    ...,
]

This setup allows django_signposting to extract JSON-LD embedded in HTML <script type="application/ld+json"> tags and add the corresponding signposting headers. It’s compatible with tools that provide JSON-LD, such as django-json-ld.

Note: The middleware order is important! Place SignpostingMiddleware before JsonLdSignpostingParserMiddleware to ensure proper extraction and processing of JSON-LD content.

Manual signposting

For cases where JSON-LD is not embedded, or you want to specify headers manually, you can use the add_signposts utility.

  1. Add Middleware: Add the SignpostingMiddleware to your Django project's MIDDLEWARE setting in settings.py:
MIDDLEWARE = [
    ...,
    'django_signposting.middleware.SignpostingMiddleware',
    ...,
]
  1. Add Signposts to your Views: Use the add_signposts utility function:
from django.http import HttpResponse
from django_signposting.utils import add_signposts
from signposting import Signpost, LinkRel

def my_view(request):
    response = HttpResponse("Hello, world!")
    
    # Add signpostings as string
    add_signposts(
        response,
        Signpost(LinkRel.type, "https://schema.org/Dataset"),
        Signpost(LinkRel.author, "https://orcid.org/0000-0001-9447-460X")
        Signpost(LinkRel.item, "https://example.com/download.zip", "application/zip")
    )

    return response

Signposts are formatted and added as Link headers by the middleware

curl -I http://localhost:8000
HTTP/2 200 
...
link: <https://schema.org/Dataset> ; rel="type" ,
      <https://orcid.org/0000-0001-9447-460X> ; rel="author" ,
      <https://example.com/download.zip> ; rel="item" ; type="application/zip"

TODO

  • Option to add signposts in HTML via elements.

License

Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_signposting-0.10.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

django_signposting-0.10.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file django_signposting-0.10.1.tar.gz.

File metadata

  • Download URL: django_signposting-0.10.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for django_signposting-0.10.1.tar.gz
Algorithm Hash digest
SHA256 0faf259cde02a81f1159631cab38f28e8a1182104fb62f5b2d321ff363e44e74
MD5 fdd334b5effafb1c031d5088fdc4b7d3
BLAKE2b-256 5fbe95e27d1f52be1da314fbf69e4cfcc57a8f3a0515673107e5af2ac988599f

See more details on using hashes here.

File details

Details for the file django_signposting-0.10.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_signposting-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 05a70e226bd88e1d396a34150d66c1635f93159544772c6c6649d0fba58a0961
MD5 2d4d70ccd34cd14d3d93e26b9284aeeb
BLAKE2b-256 11052ae593532be74989c7b9809807aa74a3cb10a1b6e35ffd693d9f687d25b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page