'Django multi-domain view set up / Django Sites. help needed

I'm trying to understand how to set up a Multisite codebase with Django (no external addons like Wagtail).

I created several dummy domains and registered them on etc/hosts "example.com", "example2.com". They now both respond as if it's "localhost".

I'm trying to find a way to make them go to different pages/views according to their different domains. I went through https://docs.djangoproject.com/en/4.0/ref/contrib/sites/ Sites documentation and am still stuck.

The view that I'm trying to make work is this:

def my_view(request):
    # current_site = request.get_host
    current_site = Site.objects.get_current()
    if current_site.domain == 'example2.com':
        html = "<html><body>Hello World at Example2 two</body></html> " + "current_site.domain=" + current_site.domain
        Site.objects.clear_cache()
        return HttpResponse(html)
        pass
    elif current_site.domain == 'example.com':
        html = "<html><body>Hello World at Example1 one</body></html> " + "current_site.domain=" + current_site.domain
        Site.objects.clear_cache()
        return HttpResponse(html)
        pass
    else:
        html = "<html><body>Hello World</body></html> " + "current_site.domain=" + current_site.domain
        Site.objects.clear_cache()
        return HttpResponse(html)
        pass

I also tried changing the domain like below(It was done within a python shell within a pipenv shell), however it doesn't change it automatically by the domain address and will set it permanently to whichever one I saved:

((django-todo-react) ) ➜  backend git:(master) ✗ ./manage.py shell
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.sites.models import Site
>>> one = Site.objects.all()[0]
>>> one.domain = 'example.com'
>>> two = Site.objects.all()[0]
>>> two.domain = 'example2.com'
>>> one.save()
>>> two.save()
>>> 

view not responding to domain

View set after one.save()^

Any suggestions on what I could do to make each domain points to their own specific view?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source