Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using subdomains in django

Tags:

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/

like image 469
Иван Янковой Avatar asked Jul 02 '15 07:07

Иван Янковой


People also ask

Why do we use subdomains?

By implementing subdomains on your website, you can create separate sections of content and services for your website without having to create new domain names for each part. Subdomains will also make it easier for users to find what they need, all in one place.

What is true about a subdomain?

To recap, a subdomain is the portion of a URL that comes before the “main” domain name and the domain extension. For example, docs.themeisle.com . Subdomains can help you divide your website into logical parts or create separate sites, for example a separate blog for each sports team.


2 Answers

You have a number of options depending on how in-depth you want to go.

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

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

like image 128
Matt Seymour Avatar answered Oct 13 '22 15:10

Matt Seymour


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'), ) 
like image 28
Denis Avatar answered Oct 13 '22 14:10

Denis