Automatic subdomain for each user

316 views
Skip to first unread message

Sherif Adigun

unread,
Dec 29, 2021, 6:02:16 PM12/29/21
to Django users
I am faced with a requirement where each user is required to use the application under his own subdomain. Whenever a user registers, he gets username.domain.com and he can add staff, manage reports, etc under his subdomain.

What is the best approach to you can suggest please? 

Tim Chase

unread,
Dec 29, 2021, 7:10:17 PM12/29/21
to django...@googlegroups.com
Generally this requires

1) configuring your DNS to allow for wildcard domains so that
*.domain.com points at your web server, usually looking something like

*.example.com. 3600 IN A 198.51.100.17

2) setting up your HTTPS certificates. This could be

- involve an ACME-aware Django route (I'm not sure if such a beast
exists)

- configuring an ACME client like certbot to update your
DNS records so that Let's Encrypt can verify that you own the
domain and then grant your a wildcard cert

- use a different CA to obtain a wildcard cert

2b) if you used a more manual method, put the certs in the
appropriate place for your web-server to pick them up

3) configure your web-server front-end/proxy (usually nginx or
Apache, but there are others) to handle the wildcard domains and pass
them through to your Django app or WSGI server or what have you. Make
sure the domain information is passed through in the HTTP_HOST header

4) use django-subdomains[1] (or maybe django-hosts[2]) middleware to
extract the intended host-name from the HTTP_HOST header and possibly
make the route reversable so that .reverse() works

5) check your views so that they make use of the HTTP_HOST
information to filter for user-specific information

-tkc


[1] https://django-subdomains.readthedocs.io/en/latest/


[2] https://github.com/jazzband/django-hosts





Sherif Adigun

unread,
Dec 30, 2021, 4:28:27 AM12/30/21
to Django users
Thank you @Tim.  Django subdomains looks promising but it's outdated. It uses codes and imports of django< v2  I have checked django-hosts but it looks as if it can be used  for only predefined list of subdomains. Please help clarify this doubt.

Thank you for your time

Sherif Adigun

unread,
Dec 30, 2021, 1:07:09 PM12/30/21
to django...@googlegroups.com
Thank you very much for you guidance. I have been able to make it work using Django-hosts on my local. Still trying to fix it out to work on my shared hosting if possible

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/CTynQlthabY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6057efaf-5e10-498a-ad2a-40132425412dn%40googlegroups.com.

Sherif Adigun

unread,
Dec 30, 2021, 3:03:44 PM12/30/21
to django...@googlegroups.com
Thank you for your detailed response. Following your response point by point with few additional googling, it works as expected.

On cpanel:
I created a subdomain called *
Then I chose document root as the main Django root. (This is needed so that letsencrypt can automatically create a certificate.)

Then the routing works seemlessly

Thank you

Nick Farrell

unread,
Dec 30, 2021, 5:29:12 PM12/30/21
to Django users
Once you have the TLS certificates and routing working, can't you make a tiny piece of middleware which takes the hostname (provided via nginx/apache in a request header) and add an attribute on the request which corresponds to your virtual site?

e.g. (dodgy untested code follows):
def multisite_middleware(get_response): def middleware(request): domain_name = request.META.get('HTTP_HOST', DEFAULT_DOMAIN)
request.my_site = Site.objects.get(id=domain_name) response = get_response(request) return response return middleware

Then anywhere in your code you can access request.my_site for the site-specific data you need.

In addition, if you haven't already, take a look at https://docs.djangoproject.com/en/4.0/ref/contrib/sites/

Joel Tanko

unread,
Dec 30, 2021, 10:42:54 PM12/30/21
to django...@googlegroups.com
Check out the django-hosts package, the docs are pretty explanatory and easy to use. 
You'd also want to add '.yourdomain' to the allowed_hosts list and it doesn't work with '127.0.0.1' as its harder for if to figure out the sub-domain, if you want to use it on your development server use localhost, e.g mrj.localhost:8000

On Thu, Dec 30, 2021, 12:02 AM Sherif Adigun <adigun...@gmail.com> wrote:
I am faced with a requirement where each user is required to use the application under his own subdomain. Whenever a user registers, he gets username.domain.com and he can add staff, manage reports, etc under his subdomain.

What is the best approach to you can suggest please? 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f330ad74-75c1-4244-ae27-aa6b97717940n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages