Hello,
I use the sites framework for a Django project that serves many different domains. But each of those domains has some templates overridden.
If I used a different instance of Django (i.e. a different gunicorn service) for each domain, I'd have different settings for each site. But I'm using a single instance to serve all sites and, wherever I need to, I use things that extract the domain from the request, like `get_current_site()`. But this doesn't seem to work with templates. A custom template loader won't work without a fight because its methods don't accept the request object as an argument.
All solutions I've thought about are ugly hacks.
The least ugly seems to be to create middleware that stores the request object in a contextvar. But it strikes me that I can't find a simpler solution for this. Is what I want that uncommon, or am I missing something?
There's an old closed "wontfix" ticket about this but without adequate explanation.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/175b4601-2038-479b-9581-48d9c196df7cn%40googlegroups.com.
-- We recommend signal.org Signed email is an absolute defence against phishing. This email has been signed with my private key. If you import my public key you can automatically decrypt my signature and be sure it came from me. Your email software can handle signing.
You're right, using a single Django instance to serve multiple sites with overridden templates can be tricky. While a custom template loader might seem appealing, it's true that it doesn't accept the request object by default. Here's the breakdown and some alternative solutions:
The Challenge:
Less Ugly Solutions:
Template Name Prefixing:
domain1/home.html
and domain2/home.html
.get_current_site
).Template Inheritance with Context Processors:
Third-Party Packages:
django-sites-templates
or django-multihost-template-loader
.Middleware with Context Variables (Least Ugly Hack):
Choosing the Best Approach:
The best solution depends on your project's complexity and preference. Here's a quick guide:
Remember, the key is to find a balance between simplicity and maintainability. Leverage built-in features and explore third-party packages before resorting to complex hacks.
I'm available if you want further assistance or questions about it😎
Have a good day ahead.Â
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/befa4ac2-40b8-44da-87c2-cbbe3ed307c6%40dewhirst.com.au.