django-subdomains2
django-subdomains2 is a fork of the no longer managed django-subdomain repo.
Tested on Python 3.8, 3.9, 3.10 and Django 3.2, 4.0
Installation
pip install django-subdomains2
Quick Start
-
Add
subdomains.middleware.SubdomainURLRoutingMiddlewareto yourMIDDLEWAREin your Django settings file. If you are usingdjango.middleware.common.CommonMiddleware, the subdomain middlware should come beforeCommonMiddlewareMIDDLEWARE = [ ... "subdomains.middleware.SubdomainURLRoutingMiddleware", "django.middleware.common.CommonMiddleware", ... ]
-
Configure the
SUBDOMAIN_URLCONFSdictionary in Django settings file.# This is the urlconf that will be used for any subdomain that is not # listed in "SUBDOMAIN_URLCONFS", or if the HTTP "Host" header does not # contain the correct domain. # If you're planning on using wildcard subdomains, this should correspond # to the urlconf that will be used for the wildcard subdomain. For example, # 'test.mysite.com' will load the ROOT_URLCONF, since it is not # defined in "SUBDOMAIN_URLCONFS". ROOT_URLCONF = "myproject.urls.frontend" # A dictionary of urlconf module paths, keyed by their subdomain. SUBDOMAIN_URLCONFS = { None: "myproject.urls.frontend", "www": "myproject.urls.frontend", "api": "myproject.urls.api", "admin": "myproject.urls.admin", }
-
Configure the
SUBDOMAIN_DOMAINin Django settings file.SUBDOMAIN_DOMAIN = "mysite.com" SUBDOMAIN_IGNORE_HOSTS = ["health-check"] # Optional, If you want to ignore the "health-check" host
Optional -
SUBDOMAIN_IGNORE_HOSTSAdd hosts toSUBDOMAIN_IGNORE_HOSTSif you need a list of hosts not to search for subdomains (which automatically uses the "None" value of SUBDOMAIN_URLCONFS) -
If you want to use the subdomain-based
{% url %}template tag, addsubdomainsto yourINSTALLED_APPS.
Basic Usage
Using Subdomains in Views
On each request, a subdomain attribute will be added to the request object. You can use this attribute to effect view logic, like in this example:
def user_profile(request):
try:
# Retrieve the user account associated with the current subdomain.
user = User.objects.get(username=request.subdomain)
except User.DoesNotExist:
# No user matches the current subdomain, so return a generic 404.
raise Http404
Resolving Named URLs by Subdomain
Included is a subdomains.utils.reverse() function that responds similarly to django.core.urlresolvers.reverse(), but accepts optional subdomain and scheme arguments and does not allow a urlconf parameter.
If no subdomain argument is provided, the URL will be resolved relative to the SUBDOMAIN_URLCONFS[None] or ROOT_URLCONF, in order. The protocol scheme is the value of settings.DEFAULT_URL_SCHEME, or if unset, http:
>>> from subdomains.utils import reverse
>>> reverse('home')
'http://example.com/'
>>> reverse('user-profile', kwargs={'username': 'ted'})
'http://example.com/users/ted/'
>>> reverse('home', scheme='https')
'https://example.com/'
For subdomains, the URL will be resolved relative to the SUBDOMAIN_URLCONFS[subdomain] value if it exists, otherwise falling back to the ROOT_URLCONF:
>>> from subdomains.utils import reverse
>>> reverse('home', subdomain='api')
'http://api.example.com/'
>>> reverse('home', subdomain='wildcard')
'http://wildcard.example.com/'
>>> reverse('login', subdomain='wildcard')
'http://wildcard.example.com/login/'
If a URL cannot be resolved, a django.urls.exceptions.NoReverseMatch will be raised.
Resolving Named URLs in Templates
The subdomainurls template tag library contains a url tag that takes an optional subdomain argument as it’s first positional argument, or as named argument. The following are all valid invocations of the tag:
{% load subdomainurls %}
{% url 'home' %}
{% url 'home' 'subdomain' %}
{% url 'home' subdomain='subdomain' %}
{% url 'user-profile' username='ted' %}
{% url 'user-profile' subdomain='subdomain' username='ted' %}
If request is in the template context when rendering and no subdomain is provided, the URL will be attempt to be resolved by relative to the current subdomain. If no request is available, the URL will be resolved using the same rules as a call to subdomains.utils.reverse() without a subdomain argument value. An easy way to ensure this functionality is available is to add django.core.context_processors.request() is in your settings.TEMPLATES["OPTIONS"]["context_processors"] list.
Test
tox
Deploy
pip install build setuptools wheel
python -m build
twine upload dist/*
Metadata
Release files for django-subdomains2 4.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-subdomains2-4.1.2.tar.gz | 5.9 kB | Without provenance | Details for django-subdomains2-4.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_subdomains2-4.1.2-py3-none-any.whl | Python 3 | none | any | Details for django_subdomains2-4.1.2-py3-none-any.whl |
Total release size: 13.0 kB
Release files / django-subdomains2-4.1.2.tar.gz
| Download URL | django-subdomains2-4.1.2.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2d8de177e89d9aa73d0d5d4e3b5ecbe7b1029eadb580fcc9b0a3632e9952103c
|
|
BLAKE2b-256 checksum How to use checksums |
cc1f9571afeb4394ce9dffa0d207b4098fc3e1e5afba3113c18c21f029f13f31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.13
|
Release files / django_subdomains2-4.1.2-py3-none-any.whl
| Download URL | django_subdomains2-4.1.2-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d317cf53ed4329c4a27f7b20bfd8bb5b3267681a766732169723a22d6855cc81
|
|
BLAKE2b-256 checksum How to use checksums |
7b21ae0384cb3bb9b39a39fe656b1d00848333e09643198528f223e0cf06a577
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.13
|
Security for django-subdomains2 4.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.