Using subdomains in django

Please tell me if it is possible (if so, how) to use each custom subdomain for pages. For example, now I have a form url: http://hostname.com/user/1 I need to get http://username.hostname.com/

+3


source to share


1 answer


You have a number of options, depending on how you drill down.

  • One option is to handle routing at the web server level. Basically you will grab part of the subdomain of the URL and rewrite it somewhere else on your server.

    For example http://username1.local.host/signin

    will be hijacked by your web server and internally redirected to a resource like /username1/signin

    . The end user will own subdomains, but your code will handle portions of the url, but no wiser about what happened.

    Your urls.py will now handle this like any normal request.

    url_pattern = [
       ...
       url(r'(?P<subdomain>[a-z]+)/sigin/$', 'view'),
    ]
    
          

    For Nginx, you will need to examine "subdomain to overwrite subdirectories".

    I would personally use this option for what you stated in your question. Although this method is a bit tricky to set up in the beginning (stick with it until it works). In the long run, it will be much easier to maintain and operate. I would personally use this option for what you stated in your question.

  • Another option is to handle subdomains at the django level with a package like Django Subdomains (I've used this one in the past and found this to be my preferred option (in terms of handling subdomains in django code)). Without going into details, nginx will grab subdomains and route the whole thing to django. Django then handles the subdomains at the middleware level.



Personally, I would use option 1 for your use. Option 2 - if you want different applications to be in different domains, for example: blog.local.host

, support.local.host

.

+8


source







All Articles