Skip to main content

Django 1.6+ library for setting your ALLOWED_HOSTS based on the domains in django.contrib.sites and the server’s public IP address which is determined at runtime.

https://travis-ci.org/kezabelle/django-allowedsites.svg?branch=master

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

from allowedsites import AllowedSites
ALLOWED_HOSTS = AllowedSites(defaults=('mytestsite.com',), dynamic_public_ip=False, net_timeout=5)
  • dynamic_public_ip=False

    If set True, the server’s public IP address is set for ALLOWED_HOSTS at runtime.

    The default is False.

  • net_timeout=5

    The timeout for the network request to get the server’s public IP address.

    The default is 5 seconds.

Or, if you want to use your cache backend:

from allowedsites import CachedAllowedSites
ALLOWED_HOSTS = CachedAllowedSites(cache_timeout=None)
  • cache_timeout=None

    For how long the cache will last, in seconds.

    The default is None, means cache forever.

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 clear_cache class method and an update_cache instance 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.clear_cache, sender=Site,
                  dispatch_uid='clear_allowedsites')

Or:

from django.conf import settings
from django.db.models.signals import post_save
from django.contrib.sites.models import Site
post_save.connect(settings.ALLOWED_HOSTS.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-dcors (Cross-Origin Resource Sharing headers) and others. I don’t know.


Copyright (c) 2014, Keryn Knight All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project.

This license applies to version 0.1.0 of django-allowedsites.

Metadata

Release files for django-allowedsites-dynamic 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-allowedsites-dynamic 0.2.1
File Size Uploaded
django_allowedsites_dynamic-0.2.1.tar.gz 5.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for django-allowedsites-dynamic 0.2.1
File Interpreter ABI Platform
django_allowedsites_dynamic-0.2.1-py3-none-any.whl Python 3 none any Details
django_allowedsites_dynamic-0.2.1-py2-none-any.whl Python 2 none any Details

Total release size: 17.6 kB

Release files / django_allowedsites_dynamic-0.2.1.tar.gz

Download URL django_allowedsites_dynamic-0.2.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
048c175baec98facf09621403a42394ab6bd0ada58513e483648fd5c4d469025
BLAKE2b-256 checksum
How to use checksums
0594fa04ef06dd07cbfdc82782512cc0a757776f300cbaffc180ddc14a0ac61b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.0

Release files / django_allowedsites_dynamic-0.2.1-py3-none-any.whl

Download URL django_allowedsites_dynamic-0.2.1-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3e2b9d869989bd2513235fc75d7e3b863f6d479f5de42d2057acf198489bf76a
BLAKE2b-256 checksum
How to use checksums
c0095db941a82b19c94df442bfc6547adfbfd39857ed286670f57baf278b032d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.0

Release files / django_allowedsites_dynamic-0.2.1-py2-none-any.whl

Download URL django_allowedsites_dynamic-0.2.1-py2-none-any.whl
Size 6.1 kB
Tags Python 2
SHA-256 checksum
How to use checksums
a2c47347247abaef04e552cbf513b2ff904323f3f956eeddad4cdccd010a096f
BLAKE2b-256 checksum
How to use checksums
888c94ac6dfd13d71c26be4529d4b7606a2a7ea56e37a618f10c39f7a0a56cc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.0

Release history Release notifications | RSS feed

This release

0.2.1 This release

3 release files

0.2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page