How do i detect the current domain name in django?

django set site name
django domain name
django get_current
django view on site
django template site
django site-packages
django get domain without request

I want to design my web application to serve two different front-end templates depending on what domain the user enter my django application.

So if the user enter, it would serve front-end from app AAA_USA, but if the user enter from, it would serve front-end from app AAA_MY.

What is the best way to do this? I was thinking of "detecting the current domain name" and then simply add an if-else statements in the View functions.

These two domains will be pointing to the same Nameservers that contains my Django app.

The way i did it is basically using the middleware (using session and detect the HTTP_HOST.

class SimpleMiddleware(object):
def __init__(self, get_response):
    self.get_response = get_response
    # One-time configuration and initialization.

def __call__(self, request):
    # Code to be executed for each request before the view (and later middleware) are called.

    # sets to show Taiwan or Indo version
    # sets the timezone too
    http_host = request.META['HTTP_HOST']
    if(http_host == ''):
        request.session['web_to_show'] = settings.TAIWAN
        request.session['timezone_to_use'] = settings.TAIWAN_TIMEZONE
        request.session['web_to_show'] = settings.INDO
        request.session['timezone_to_use'] = settings.INDONESIA_TIMEZONE

    response = self.get_response(request)

    # Code to be executed for each request/response after the view is called.

    return response

The “sites” framework | Django documentation, If the setting is omitted, the get_current_site() function will try to get the current site by comparing the domain  Whether You're Ready to Launch or Parking Your Domain for Later, We've Got You Covered.



will retrive the full path: es:


URL dispatcher | Django documentation, This module is pure Python code and is a mapping between URL path this is the algorithm the system follows to determine which Python code to execute: If there is a current application defined, Django finds and returns the URL resolver​  Find Domain Names now. Visit & Look for More Results!

If you don't have access to the requests object, then you can use the sites framework:

from django.contrib.sites.shortcuts import get_current_site
domain = get_current_site(None)

This will return the site the Django is configured for (using the value saved in the django_site table of your database. (See:

Django Tips #7 How to Get the Current URL Within a Django Template, Troubleshooting. Django 1.7 or below. If you are using an older version of Django (<= 1.7) where the TEMPLATES configuration is not  Buy a domain name from the most trusted name in domains – Network Solutions®. Everything your new website needs - affordable domains, reliable hosting & more.

How To Use Dynamic Subdomains In Django - Crowdbotics, python createdomain. Creating Middleware to Detect Current Domain​: So far we've created a model to define domain names, and a CLI command to  You can use a combination of the native Sites framework and get_absolute_url instead. Set up at least one Site in the admin, make sure your model has a get_absolute_url () method, then: >>> from django.contrib.sites.models import Site >>> domain = Site.objects.get_current

Django allowed_hosts explained, Typically, the host header is used when using Apache virtual hosts or Nginx server blocks to determine which application should handle the  Final Thoughts. When you are developing a responsive website with Django and need to check how it works on a mobile device, you can run the development server with and access it on your Wifi network through the IP address of your computer, or you can use to analogically check it by a domain name.

Sams Teach Yourself Django in 24 Hours, The admin framework uses the current Site object to determine which domain the view or site link is redirected to. . The comments framework uses the sites  It’s a hook for associating objects and functionality to particular websites, and it’s a holding place for the domain names and “verbose” names of your Django-powered sites. Use it if your single Django installation powers more than one site and you need to differentiate between those sites in some way.

  • Use {{ request.get_host }}
  • thank you, but is there anyway to do it without request object? because i may also need to use it outside of view functions (that contains the request argument)
  • check this: Site.objects.get_current().domain
  • Possible duplicate of detect the HOST domain name in django models
  • Is there a way to do it without using the Site package? if no, thanks, i will try it and see if it's doable.
  • is there anyway to do it without request object? because i may also need to use it outside of view functions (that contains the request argument)
  • without the request object is impossible. 2 ways 1) Pass as argument the request object in the other functions 2) in the first view save the value of the path in a cookie and read the cookie in the other functions