Package: django-subdomains2
django-subdomains2django-subdomains2 is a fork of the no longer manageddjango-subdomainrepo.Tested on Python 3.8, 3.9, 3.10 and Django 3.2, 4.0Installationpipinstalldjango-subdomains2Quick StartAddsubdomains.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 theSUBDOMAIN_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 theSUBDOMAIN_DOMAINin Django settings file.SUBDOMAIN_DOMAIN="mysite.com"SUBDOMAIN_IGNORE_HOSTS=["health-check"]# Optional, If you want to ignore the "health-check" hostOptional -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 UsageUsing Subdomains in ViewsOn each request, asubdomainattribute will be added to therequestobject. You can use this attribute to effect view logic, like in this example:defuser_profile(request):try:# Retrieve the user account associated with the current subdomain.user=User.objects.get(username=request.subdomain)exceptUser.DoesNotExist:# No user matches the current subdomain, so return a generic 404.raiseHttp404Resolving Named URLs by SubdomainIncluded is asubdomains.utils.reverse()function that responds similarly todjango.core.urlresolvers.reverse(), but accepts optionalsubdomainandschemearguments and does not allow aurlconfparameter.If nosubdomainargument is provided, the URL will be resolved relative to theSUBDOMAIN_URLCONFS[None]orROOT_URLCONF, in order. The protocol scheme is the value ofsettings.DEFAULT_URL_SCHEME, or if unset,http:>>>fromsubdomains.utilsimportreverse>>>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 theSUBDOMAIN_URLCONFS[subdomain]value if it exists, otherwise falling back to theROOT_URLCONF:>>>fromsubdomains.utilsimportreverse>>>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, adjango.urls.exceptions.NoReverseMatchwill be raised.Resolving Named URLs in TemplatesThesubdomainurlstemplate tag library contains aurltag that takes an optionalsubdomainargument 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' %}Ifrequestis 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 tosubdomains.utils.reverse()without asubdomainargument value. An easy way to ensure this functionality is available is to adddjango.core.context_processors.request()is in yoursettings.TEMPLATES["OPTIONS"]["context_processors"]list.TesttoxDeploypip install build setuptools wheel
python -m build
twine upload dist/*