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 hashes)

Uploaded Source

Built Distribution

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