5

We're expanding our business into Europe and I'm using Mezzanine's multi-tenancy feature to host both the US and EU versions of the site on the same Django installation. We have a /locations page on each site that I would like to serve with different templates, based on the SITE_ID.

I've followed Mezzanine's sparse documentation here and added the following to settings.py

HOST_THEMES = [
    ('domain.com', 'domain_app'), 
    ('domain.eu', 'domain_eu')
]

I've added domain_eu to INSTALLED_APPS after the base theme and used python manage.py startapp domain_eu to generate the directory and manually created the domain_eu/templates/pages/locations.html file.

I then copied the locations page and assigned it to the EU site.

The page still renders with the locations template located in the base theme domain_app/templates/pages/locations.html

I've confirmed that the correct SITE_ID is set in the request.

How do I get the page to render with the template in its corresponding theme/app directory based on the current SITE_ID?

Robert Carter Mills
  • 793
  • 1
  • 9
  • 19

1 Answers1

4

After digging into Mezzanine's code I figured out why my eu theme template was not rendering. The crucial bit of code can be found in mezzanine/utils/sites.py

def host_theme_path(request):
    """
    Returns the directory of the theme associated with the given host.
    """
    for (host, theme) in settings.HOST_THEMES:
        if host.lower() == request.get_host().split(":")[0].lower():

When I logged the result of request.get_host() I quickly realized the problem because it was localhost which obviously would not match any of the HOST_THEMES domains.

I had assumed that Mezzanine would use the session variable site_id as per their documentation but apparently not down this particular template rendering code path.

The solution therefore was simply to edit my /etc/hosts file with the following:

127.0.0.1 domain.eu

Now when I visit domain.eu/locations it renders the template from the correct theme directory (in a local dev environment)

Robert Carter Mills
  • 793
  • 1
  • 9
  • 19