Skip to main content

Django Site Alias

A Django App to add support for domain aliases to entries in Django’s Site framework.

The Problem

For example, your platform might create the FQDN for your customer like this:

customer.myplatform.com

They might want to use the domain name of:

customersite.com

The problem with Django’s site framework is: 1 Domain = 1 Site ID

There are two ways to solve this. The first is to create an instance per customer that manually sets the SITE_ID. They then go to that instance running the app and it works. The downside is that you are provisioning a server per customer, which can get inefficient at scale.

What if you have multiple customers but waitn to maintain only one server?

The alternate to setting a SITE_ID explicitly is to use Django’s middleware to figure out the site from the request. This is fine, if there is only one possible domain per customer.

Solution

To try to resolve this issue we created some lite ‘wrapper’ code around the Site framework to provide an alias lookup.

It’s in a seperate model that you can let your customers update if you want (via a customer admin panel for example), letting them update as they want while keeping the Site model out of their hands.

Setup

To install run the following in a shell:

pip install django-site-alias

then add ‘sitealias’ to your django project’s list of apps and run migrate to get the new model. Make sure to also include ‘django.contrib.sites’ since this is just a wraper around that code.

Builtin View Mixins

PassRequestToFormKwargsMixin - adds the current request to the form kwargs

SetSiteFromRequestFormValidMixin - sets the current site to the self.object.site with in the form_valid of any generic editing view

SiteQuerysetMixin: - Filters model by current site found in the request using get_queryset method

Built-in managers

from sitalias.models import RequestSiteManager

class Model(models.Model):
 #......
    objects = RequestSiteManager()

Will add the following chainable methods to the objects filter:

Model.objects.from_site(site)

or

Model.objects.from_request(request)

Included manager serves as a suggestion, feel free to build your own implementation.

Roadmap

  • [x] Middleware - sitealias.middleware.CurrentSite - mimics to django.contrib.sites.middleware.CurrentSite except that it adds current site to request object site via Sitealias model, before checking the Site table

  • [x] Shortcut - from sitealias.shortcuts import get_current_site - mimics django.contrib.sites.shortcuts.get_current_site but checks sitealias table before checking the Site model

  • [] Callables for the ALLOWED_HOSTS setting (perhaps a subclass of the AllowedSites callable in django-allowedsites ?? )

    • [] CachedAllowedSitesAndAlias

    • [] AllowedSitesAndAliass ??

INSTALLED_APPS = [
    ...
    'django.contrib.sites',
    ...
    'sitealias',
    ...
]

Then in your Django settings, add the following to your middleware:

MIDDLEWARE = [
    ...
    'sitealias.middleware.CurrentSiteMiddleware',
    ...
]

This is meant to be a drop-in replacement for Django’s ‘django.contrib.sites.middleware.CurrentSiteMiddleware’. It will look for a site that matches an alias first, then fall back to Django’s code.

Metadata

Release files for django-site-aliases 0.1.2

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-site-aliases 0.1.2
File Size Uploaded
django-site-aliases-0.1.2.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-site-aliases 0.1.2
File Interpreter ABI Platform
django_site_aliases-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / django-site-aliases-0.1.2.tar.gz

Download URL django-site-aliases-0.1.2.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
32f71b79b05bf5c8e4aa768c284e658a748d956c984321900c5c2678f227b7f1
BLAKE2b-256 checksum
How to use checksums
b50dcb7ff2c6dfdd34db5fa271c893b481edb2c58a2972aea5b5192d6084285d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.12

Release files / django_site_aliases-0.1.2-py3-none-any.whl

Download URL django_site_aliases-0.1.2-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
32d2e461e6bed8d788724086997516f9b8f5502b618288a4c073051704ef1b29
BLAKE2b-256 checksum
How to use checksums
da21ca3b125980d0cdd5617d4375fb8a8dcb0b4e394d426f4a8b7d1c71ec77e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.12

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.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