get current domain name django

To get the current domain name in Django, you can follow these steps:

  1. Import the django.contrib.sites.models module: This module provides the Site model, which represents a website in Django. To access the current domain name, you need to import this module.

  2. Use the Site.objects.get_current() method: This method retrieves the current site from the database. It returns an instance of the Site model representing the current site.

  3. Access the domain attribute of the Site instance: Once you have the current site instance, you can access its domain attribute to get the domain name.

Here's an example of how you can implement these steps in your Django project:

from django.contrib.sites.models import Site

def get_current_domain():
    current_site = Site.objects.get_current()
    domain = current_site.domain
    return domain

By calling the get_current_domain() function, you will get the current domain name of your Django project.