Skip to main content

Dynamic ALLOWED_HOSTS based on the configured django.contrib.sites

Project description

============================= django-dynamic-allowedsites

Django 1.6+ library for setting your ALLOWED_HOSTS based on the domains in django.contrib.sites

.. image:: https://travis-ci.org/frankyjquintero/django-allowedsites.svg?branch=master :target: https://travis-ci.org/frankyjquintero/django-allowedsites

Usage is something like the following, in your settings.py or equivalent::

from allowedsites import AllowedSites
ALLOWED_HOSTS = AllowedSites(defaults=('mytestsite.com',))

Or, if you want to use your cache backend::

from allowedsites import CachedAllowedSites
ALLOWED_HOSTS = CachedAllowedSites()

A single key, allowedsites will be inserted containing an unsorted collection of all the domains that are in the django.contrib.sites. For the sake of allowing multiple processes to keep up to date with the Site values without hitting the database, using a shared cache (ie: not LocMemCache) is encouraged.

The CachedAllowedSites also provides an update_cache class method which may be used as a signal listener::

from django.db.models.signals import post_save
from django.contrib.sites.models import Site
post_save.connect(CachedAllowedSites.update_cache, sender=Site,
                  dispatch_uid='update_allowedsites')

You can modify the the defaults::

from allowedsites import AllowedSites
ALLOWED_HOSTS = AllowedSites(defaults=('mytestsite.com',))
ALLOWED_HOSTS += AllowedSites(defaults=('anothersite.net',))
ALLOWED_HOSTS -= AllowedSites(defaults=('mytestsite.com',))
# ultimately, only anothersite.net is in the defaults

Other uses?

It may work with django-csp_ (Content Security Policy headers), django-cors-headers_ Cross-Origin Resource Sharing (CORS) headers and others. I don't know.

.. _django-csp: https://github.com/mozilla/django-csp .. _django-cors-headers: https://github.com/adamchainz/django-cors-headers

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-dynamic-allowedsites-0.2.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_dynamic_allowedsites-0.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file django-dynamic-allowedsites-0.2.0.tar.gz.

File metadata

  • Download URL: django-dynamic-allowedsites-0.2.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4

File hashes

Hashes for django-dynamic-allowedsites-0.2.0.tar.gz
Algorithm Hash digest
SHA256 147dd7d4c2b11ff1949ca345d942278699f91489a246cb9d0891a2a139f2bf8c
MD5 704f55a75bd5c16f798b301c853ca160
BLAKE2b-256 cfd4c5b31cda6af7f237ccd81c66d96d2b49038b408255e65ad6dd783de84cd8

See more details on using hashes here.

File details

Details for the file django_dynamic_allowedsites-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_dynamic_allowedsites-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4

File hashes

Hashes for django_dynamic_allowedsites-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a5e34705aa4c214cd88856fdbc987bb8fe54a3252328b0a8490d84c19e200ff
MD5 558f7a3c5108a10632a53530f04d60bc
BLAKE2b-256 c5fe631f40f4e01810a2bd29e516ba25b1bd26786dbecb4b87ca9304e3b15d2b

See more details on using hashes here.

Supported by

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