2

We've just started the complete redesign of our project's HTML pages (responsive layout and stuff) and all we need to change is templates and static (css and some js). Views ain't gonna change at all. I want to create all the new templates in another folder next to existing old one, e.g. "new templates", so when the work is complete we can just change our TEMPLATE_DIRS setting and that's it.

But obviously we want to let our users (for the first couple of weeks after deploying it) choose if they want to try the new version or stay with the old one. Ok, that's simple - I ask a user and put his answer into his session: request.session['new_design'] = True

And now in some custom middleware it would be nice to write something like this:

if request.session.get('new_design', False):
    settings['TEMPLATE_DIRS'] = (MY_NEW_TEMPLATE_DIR, ) + settings['TEMPLATE_DIRS']

So when it comes to template loading in whatever view, my brand new templates folder will be searched first. But I know I just can't modify settings on the fly!

Is there any other way to archive same results? I thought about subclassing filesystem.Loader... But how can I make it aware of current request/session contents? Any other suggestions?

UPD: I forgot to mention: subdomains are already used for city selection e.g. la.domain.tld would represent only objects available for LA users. So adding a fourth subdomain isn't that great.

ps And once again just to make it clear: the main goal is not to touch any view!

Sleamey
  • 25
  • 4
  • You probably need to change the templates just for a couple applications. Keep the same parent template folder (the one in settings) and add the rest of the path programmatically. You could have something like: Templates/site-templates/old-design/ and Templates/site-templates/new-design/. In a view, add a mixin that selects the path depending on the request. – cdvv7788 Feb 26 '15 at 15:42
  • there are too much views (and most of them shamefully aren't class-based) to edit every single one injecting path selection. – Sleamey Feb 26 '15 at 18:49

1 Answers1

1

I think you must create your own template Loader (see https://docs.djangoproject.com/en/1.7/ref/settings/#template-loaders ). As you need to have the request in your custom Template loader, you can have it as a global value as explained here : https://stackoverflow.com/a/27694861/983222

Edit to add another solution :

Configure a subdomains (eg : new.domain.tld) which use an other settings.py with your new template dir. If you use multi domain cookies to avoid a session lose, this should work.

Community
  • 1
  • 1
DylannCordel
  • 586
  • 5
  • 10
  • Hmmm... It seems my question is kinda duplicate of mentioned one. Thanks a lot, @Squallynou . However I'm not really satisfied with the solution proposed there (global storage? oh no, I believe there should be the other way) ...but anyway it's *a solution* – Sleamey Feb 26 '15 at 18:34
  • Yeah, it's a violent solution, I agree. And what about to use a subdomain which use a different settings ? eg : domain.tld : old templates and new.domain.tld : new templates – DylannCordel Feb 26 '15 at 18:47
  • oh, I forgot to mention: subdomains are already used for city selection e.g. la.domain.tld would represent only objects available for LA users. Of course, we can add a fourth subdomain in that chain, but it's a bit harder to implement and personally this looks even worse – Sleamey Feb 26 '15 at 19:06
  • i.e. subdomain is in fact the *same global storage*, but with limited capabilities and visible to user (the last thing can be good, but in our case it's definitely not) – Sleamey Feb 26 '15 at 19:13