Getting Your Domain Name in Django Templates

When building a Django-powered website, you may need to display the site’sDomain name in templates and views. Knowing how to properly get the domain can help ensure links, images, and other external references work correctly. In this post, we’ll explore a few simple ways to get your domain name in Django templates.

Using the Sites Framework – Domain Name

Django comes with a built-in sites framework that enables associating objects and functionality to a specific site or domain. Additionally, it provides a domain name property that can be easily accessed.

First, you need to define the name and domain in your project’s settings:

# Settings.py

SITE_ID = 1

ALLOWED_HOSTS = ['www.mydomain.com'] 

Next, retrieve the current Site object in your views or templates:

{{ site.domain }}

This will output just the domain name, without any protocol or path, such as “www.mydomain.com”.

Alternatively, you can get the fullDomain with protocol:

{{ site.domain|default:"example.com" }}

This joins the protocol andDomain, outputting a completeDomain string like “http://www.mydomain.com”.

Using the request Object

Another approach is accessing the request object, which contains details about the current HTTP request.

In any Django view or template, the request is available automatically. So you can output the fullDomain and path:

{{ request.get_host }}

For just theDomain:

{{ request.get_host|split:"."|first }}

This filters out any port or path info, leaving the stand-alone domain.

Building Absolute URLs

Often theDomain name is needed to construct full, absolute URLs in templates and views. Django provides some specialized template tags and filters to make this easier.

For example, to build an absolute path to a static asset:

{% load static %}

<img src="{% get_static_prefix %}images/logo.png">

This handy tag prepends the static root URL, includingDomain, automatically.

Similarly, for media files uploaded by users:

{{ product.image.url }}    

<a href="{% get_media_prefix %}{{ product.image }}">View</a>

Django can output fully qualified URLs to media, again inserting theDomain name for you.

Using Custom Template Tags

For more advanced cases, you can create custom template tags and filters to get theDomain name.

First, register new tags in a templatetags module:

# templatetags/domains.py

from django import template

register = template.Library()

@register.simple_tag
def domain_name():
  return site.domain

Then load and use it in any template:

{% load domains %}

{{ domain_name }}

This keepsDomain logic out of the templates, making it easier to reuse.

Getting Sub domain

Sometimes you need to determine not only the domain, but also handle subdomains properly in links and redirects.

Use Python’s urlsplit() in custom template tags:

from urllib.parse import urlsplit

@register.simple_tag
def extract_subdomain(url):
  return urlsplit(url).subdomain

This would return just the subdomain section like “www” or “support”.

Conclusion

Whether building reusable apps, handling multi-tenancy, or just improving templates – access to the current domain is essential in most Django projects.

Luckily, Django offers multiple ways to get the domain name right in templates and views. The sites framework, request object, and custom tags provide flexibility. Following Django conventions results in clean, DRY code.

With these simple techniques, you can reliably reference domains and construct absolute URLs. This helps everything from static files to redirects work seamlessly across environments.

So try out a few options, see which suits your project best – and be sure your domains are handled elegantly.