Please tell me whether it is possible (if so, how) to use for pages each user subdomains. For example, now I have a URL of the form: http://hostname.com/user/1 I need to get http://username.hostname.com/
-
3This has more to do with your server configuration rather than python or django – TheGeorgeous Jul 02 '15 at 07:25
-
possible duplicate of [Django multi tenancy](http://stackoverflow.com/questions/29938338/django-multi-tenancy) – Sayse Jul 02 '15 at 07:28
-
1Note: The question in the duplicate may be different, but the answer is the same – Sayse Jul 02 '15 at 07:30
-
1you can use http://django-subdomains.readthedocs.org/en/latest/index.html – doniyor Jul 02 '15 at 07:47
-
1Configure your webserver to direct all subdomain request to your django app (catch all). Then do something with there requests like: bits = urlparse.urlsplit(request.META['HTTP_HOST'])[0].split('.') bits[0] Source: http://agiliq.com/blog/2008/10/using-subdomains-with-django/ – het.oosten Jul 02 '15 at 08:15
2 Answers
You have a number of options depending on how in-depth you want to go.
One option is to handle the routing at the web server level. Basically you will capture the subdomain part of the URL and rewrite it to a different location within your server.
For example
http://username1.local.host/signin
will be captured by your webserver and internally routed to a resource such as/username1/signin
. The end user will subdomains but your code will handle url parts be none the wiser as to what has happened.Your urls.py will then handle this like any normal request.
url_pattern = [ ... url(r'(?P<subdomain>[a-z]+)/sigin/$', 'view'), ]
For Nginx you will need to look into "subdomain to subdirectory re-writing".
I would personally use this option for what you have stated in your question. Whilst this method is a little more tricky to setup initially (keep at it until it works). It will be a lot easier to maintain and work with in the long run.
The other option is to handle the subdomains at the django level using a package such as Django Subdomains (I have used this one in the past and found it to be my preferred option (in terms of handling subdomains within django code)). Without going into too much detail nginx will capture the subdomains and route all of it to django. Django will then handle the subdomains at the middleware level.
Personally I would use option 1 for your usage. Option 2 is if you want different apps on different domains for example: blog.local.host
, support.local.host
.

- 11,052
- 9
- 47
- 104

- 8,880
- 7
- 60
- 101
-
7Just a small warning that option one would require you to enter the subdomain as a parameter into every view and potentially cause pep8 warnings for unused variables (which is what the duplicate was trying to avoid). – Sayse Jul 02 '15 at 09:55
-
1Just another small warning: option 2 is not supported, they doesn't merge https://github.com/tkaemming/django-subdomains/pull/67 – Ivan Borshchov Dec 23 '19 at 15:21
-
3[Django Subdomains](http://django-subdomains.readthedocs.org/en/latest/) seems to be abandoned, but there is an actively developped fork: [abe312/django-subdomains](https://github.com/abe312/django-subdomains), install it with `pip install subdomains`. – jammon May 01 '20 at 08:47
Consider using django-hosts
From docs:
# For example, if you own example.com but want to serve
# specific content at api.example.com and beta.example.com,
# add the following to a hosts.py file:
from django_hosts import patterns, host
host_patterns = patterns('path.to',
host(r'api', 'api.urls', name='api'),
host(r'beta', 'beta.urls', name='beta'),
)

- 940
- 12
- 16